最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Using AngularJS $resource to get data - Stack Overflow

programmeradmin4浏览0评论

I am trying to use $resource to get data from a static json file and here is the code snippet :

 angular.module('app.services', ['ngResource']).
  factory('profilelist',function($resource){
    return $resource('services/profiles/profilelist.json',{},{
        query:{method:'GET'}
    });
});

In the controller,

function ProfileCtrl($scope,profilelist) {
$scope.items = [];
$scope.profileslist = profilelist.query();
for (var i=0;i<=$scope.profileslist.length;i++){
    if($scope.profileslist[i] && $scope.profileslist[i].profileid){
        var temp_profile = $scope.profileslist[i].profileid;
    }
    $scope.items.push(temp_profile);

}

But now, I am facing an error : TypeError: Object #<Resource> has no method 'push'

Could you please help me where I am going wrong ?

I am trying to use $resource to get data from a static json file and here is the code snippet :

 angular.module('app.services', ['ngResource']).
  factory('profilelist',function($resource){
    return $resource('services/profiles/profilelist.json',{},{
        query:{method:'GET'}
    });
});

In the controller,

function ProfileCtrl($scope,profilelist) {
$scope.items = [];
$scope.profileslist = profilelist.query();
for (var i=0;i<=$scope.profileslist.length;i++){
    if($scope.profileslist[i] && $scope.profileslist[i].profileid){
        var temp_profile = $scope.profileslist[i].profileid;
    }
    $scope.items.push(temp_profile);

}

But now, I am facing an error : TypeError: Object #<Resource> has no method 'push'

Could you please help me where I am going wrong ?

Share Improve this question asked Mar 17, 2013 at 21:27 codejammercodejammer 4691 gold badge7 silver badges18 bronze badges 2
  • 1 I am not sure, but notice that .query() is not a synchronous operation, meaning you cannot iterate over the results immediately. – holographic-principle Commented Mar 17, 2013 at 22:09
  • You are right. I used a callback to iterate over the results. – codejammer Commented Mar 18, 2013 at 13:53
Add a comment  | 

1 Answer 1

Reset to default 21

You don't need to specify actions parameter for default $resource methods (these are 'get', 'save', 'query', 'remove', 'delete'). In this case you can use .query() method as is (this requires only service definition to be chaged):

angular.module('app.services', ['ngResource']).
  factory('profilelist',function($resource){
    return $resource('services/profiles/profilelist.json');
  });

P.S. And one more hint is that your example unwrapped json into hash rather then array (that's why you received no push method error), if you need it to be an array set isArray: true to action config:

'query':  {method:'GET', isArray:true}

And as @finishingmove spotted you really can't assign $resource result to obtain immediately, provide callback:

$scope.profileslist = profilelist.query(function (response) {
    angular.forEach(response, function (item) {
        if (item.profileid) {
            $scope.items.push(item.profileid);
        }
    });
});
发布评论

评论列表(0)

  1. 暂无评论