标签: restangular

如何配置Restangular URL building的Id属性

,

Restangular.one('users').getList().then(function(users) {
  $scope.user = users[0]; 
});
Run Code Online (Sandbox Code Playgroud)

另一个GET请求:

让: user: { id: '123' }

$scope.cars = $scope.user.getList('cars'); 这将触发GET请求:/ users/123/cars

其中123是从所获取id属性.


题:

是否可以明确地配置我的对象的id属性,例如:被命名为username.

user: { username: '123' }
Run Code Online (Sandbox Code Playgroud)

ajax httprequest angularjs restangular

3
推荐指数
1
解决办法
1467
查看次数

使用Restangular处理错误代码(401,403)

我正在使用Restangular构建一个应用程序.我会在一个地方实现处理错误,如401,403,500等在响应拦截器中.

这就是我写的:

RestangularProvider.setResponseExtractor(function(response, operation, what, url) {
  switch(response.meta.code) {
    case 200:
      // Handle 200
    break;

    case 401:
      // Redirect to login
    break;

    /* ... */

    default:
    break;
  }
}
Run Code Online (Sandbox Code Playgroud)

实际发生的是200正确看到但是这个方法根本没有被401命中.我从服务器端获得的JSON格式如下,以便成功响应:

{"meta":{"apiVersion":"0.1","code":200,"errors":null},"response":{"response":"value"}}
Run Code Online (Sandbox Code Playgroud)

{"meta":{"apiVersion":"0.1","code":401,"errors":[{"code":401,"message":"Unauthorized"}]},"response":null}
Run Code Online (Sandbox Code Playgroud)

为了错误.响应头也包含Status Code:401 Unauthorized.我究竟做错了什么?

angularjs restangular

3
推荐指数
1
解决办法
2888
查看次数

Restangular PUT 无缘无故附加了 _id 两次

我在客户端使用 Restangular,将 _id 作为 Id 字段。遗憾的是,Restangular 生成了错误的 URL,也许您可​​以告诉我错误在哪里?

Restangular.all('/users').one(id).get().then(functon(results) {
    $scope.data = results;
})
Run Code Online (Sandbox Code Playgroud)

用户编辑数据后:

$scope.save = function() {
     $scope.data.put().then(...);
};
Run Code Online (Sandbox Code Playgroud)

这个非常简单的示例使用两次 id 生成以下 URL。我不知道出了什么问题。:(

PUT /users/537283783b17a7fab6e49f66/537283783b17a7fab6e49f66

angularjs restangular

3
推荐指数
1
解决办法
673
查看次数

AngularJS DRY控制器结构

下面的代码表示在处理来自服务器的数据的每个控制器中重复相同代码模式的情况.经过长时间的研究和#angularjs的irc谈话,我仍然无法想象如何抽象代码,内联注释解释了这些情况:

myApp.controller("TodoCtrl", function($scope, Restangular,
                                      CalendarService, $filter){
    var all_todos = [];
    $scope.todos = [];
    Restangular.all("vtodo/").getList().then(function(data){
        all_todos = data;
        $scope.todos = $filter("calendaractive")(all_todos);
    });
    //I can see myself repeating this line in every 
    //controller dealing with data which somehow relates
    //and is possibly filtered by CalendarService:
    $scope.activeData = CalendarService.activeData;
    //also this line, which triggers refiltering when
    //CalendarService is repeating within other controllers
    $scope.$watch("activeData", function(){
        $scope.todos = $filter("calendaractive")(all_todos);
    }, true);

});

//example. another controller, different data, same relation with calendar?
myApp.controller("DiaryCtrl", function($scope, Restangular,
                                       CalendarService, …
Run Code Online (Sandbox Code Playgroud)

controller dry angularjs restangular

3
推荐指数
1
解决办法
1435
查看次数

GET中的Restangular多个ID

我正在寻找在AngularJS应用程序中从资源切换到restangular.我喜欢新模型,但我有一个问题.我无法弄清楚如何进行此API调用:

http://localhost:8000/api/entity/groups/1,2,3,4/events
Run Code Online (Sandbox Code Playgroud)

我有这样的代码:

var GroupService = ['Restangular', function (Restangular) {
    var restAngular = Restangular.withConfig(function (Configurer) {
        Configurer.setBaseUrl('/api/entity');
    });

    return {
        getEvents: function (groupIds) {
            return restAngular.all('groups', groupsIds).getList('events');
        }
    }
}];
Run Code Online (Sandbox Code Playgroud)

有没有办法要求一组组(多个组,而不是所有组),然后获取任何这些组的所有事件?

javascript angularjs restangular

2
推荐指数
1
解决办法
1080
查看次数

Angularjs - 工厂/服务内的Restangular调用

我使用factory如下:

var appModule = angular.module('appModule', ['ngRoute','restangular']);

appModule
  .config(['$routeProvider', function($routeProvider) {
    $routeProvider
      .when('/home', {
        templateUrl: 'home.html',
        controller:  'ngHomeControl'
      })
      .when('/contacts', {
        templateUrl: 'contacts.html',
        controller:  'ngContactControl'
      });
  }]);

appModule
  .factory('testFactory', function testFactory(Restangular) {
    return {
      getFriendList: function() {
        return Restangular
          .all('api/users/getfriendsinvitations/')
          .getList()
          .then(function(response) {
            //
          });
      } 
    }
  });

appModule
  .controller('ngHomeControl', function($scope, testFactory) {
    $scope.homeFriends = testFactory.getFriendList();
  });

appModule
  .controller('ngContactControl', function($scope, testFactory) {
    $scope.contactFriends = testFactory.getFriendList();
  });
Run Code Online (Sandbox Code Playgroud)

在这里,我不能获得的价值$scope.homeFriends$scope.contactFriendsRestangular电话.任何人都可以建议我如何Restangular使用factory/ 来调用值service …

scope angularjs angularjs-service angularjs-factory restangular

2
推荐指数
1
解决办法
4407
查看次数

RestAngular获取JSON对象的数组

我正在尝试使用restangular从服务器获取JSON响应.

  var baseAccounts = Restangular.one('getAllCustomers');
  baseAccounts.getList().then(function(customers) {
      $scope.myData = customers;
      console.log(customers);
    });
Run Code Online (Sandbox Code Playgroud)

问题是我总是得到以下格式的响应:

0: Object
1: Object
2: Object
addRestangularMethod: function bound() {
all: function bound() {
allUrl: function bound() {
clone: function bound() {
customDELETE: function bound() {
customGET: function bound() {
customGETLIST: function bound() {
Run Code Online (Sandbox Code Playgroud)

但我想只返回纯粹的JSON结构.如果有人在这里放置RestAngular插件的例子,我会很高兴.

谢谢你的帮助.

javascript json angularjs restangular

2
推荐指数
1
解决办法
2181
查看次数

Restangular - 如何使用GET方法访问此API?

如何使用以下签名对REST API进行GET调用:

http://www.example.com/hierarchies/nodes/1005/parents
Run Code Online (Sandbox Code Playgroud)

我试图像这样调用API:

var service = Restangular.all('hierarchies');

return service.one('nodes', id).all('parents').get();
Run Code Online (Sandbox Code Playgroud)

但它会引发以下错误:

TypeError: Cannot read property 'toString' of undefined
Run Code Online (Sandbox Code Playgroud)

API调用(如果成功)将以嵌套格式响应,如下所示:

{
    name: "",
    children: [
        {
            name: "",
            children: [
                {
                    name: "",
                    children: [
                        ..
                    ]
                }
            ]
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

提前致谢!

angularjs restangular

2
推荐指数
1
解决办法
4993
查看次数

当页面刷新或深度链接时,angular-ui-router不满足状态解析承诺

使用AngularJS 1.2.16,ui-router和Restangular(对于API服务),我有一个带有子状态的抽象状态,它使用多个视图.子视图接受URL参数(通过$ stateParams)以及根据通过URL传递的给定ID从API获取记录的决心.

当您在整个站点中导航时,一切都很完美,但是如果您在给定页面上并刷新它(Command/Ctrl + r或单击刷新按钮)或通过点击带有ID的深层链接直接加载页面(这是更重要的问题)API服务被请求命中,但承诺永远不会完成解析,因此数据不可用于状态,控制器,模板等.

根据ui-router的文档,在所有promise都解决之前,不应该实例化控制器.我在Google和SO中搜索过高低,阅读了AngularJS,Restangular和ui-router文档,以及大量的博客,并尝试了我知道的每一次迭代来解决这个问题,找不到任何指向解决方案的内容.

这是有问题的代码:

company.js(控制器代码)

angular.module('my.company', ['ui.router', 'CompanySvc'])

.config(['$stateProvider', function config ($stateProvider) {
  $stateProvider
    .state('company', {
      abstract: true,
      url: '/company',
      data: {
        pageTitle: 'Company'
      },
      views: {
        'main': {
          templateUrl: 'company/companyMain.tpl.html'
        }
      }
    })
    .state('company.landing', {
      url: '/{id}',
      views: {
        'summary': {
          controller: 'CompanyCtrl',
          templateUrl: 'company/companyDetails.tpl.html'
        },
        'locations': {
          controller: 'CompanyCtrl',
          templateUrl: 'company/companyLocations.tpl.html'
        }
      },
      resolve: {
        companySvc: 'CompanySvc',
        // Retrieve a company's report, if the id is present
        company: ['$log', '$stateParams', 'companySvc', function ($log, …
Run Code Online (Sandbox Code Playgroud)

javascript angularjs single-page-application restangular angular-ui-router

2
推荐指数
1
解决办法
3505
查看次数

Restangular - 如何获取请求的简单结果(未包装)

我试图使用mgonto的Restangular从REST api获取一些数据.

// Restangular returns promises
Restangular.all('users').getList()  // GET: /users
.then(function(users) {
  // returns a list of users
  $scope.user = users[0]; // first Restangular obj in list: { id: 123 }
})

// Later in the code...

// Restangular objects are self-aware and know how to make their own RESTful requests
$scope.user.getList('cars');  // GET: /users/123/cars
Run Code Online (Sandbox Code Playgroud)

这没关系,这很有效,但是promises回调中返回的所有结果都有Restangular添加的一些方法和属性(这是你可以对用户做的.getList("cars")).

我想要的是只检索用户的数据(name,id ...)而不使用Restangular方法.只是一个普通的JS对象.

我在文档中找不到任何方法.每次我在返回的用户上使用方法时,它总是返回一个包含Restangular方法的包装对象.

javascript angularjs ngresource restangular

2
推荐指数
1
解决办法
3771
查看次数