0

Everything works fine but I have problem getting source from service.

I have SERVICE code below.

app.factory('myService', 
    function($rootScope, ResourceData) {
    var result = {};
    var data   = ResourceData.query();

    result.getData = function() {
        // problem here
    }

    return result;
});

And CONTROLLER contain code.

app.controller('myController', ['$scope', 'myService', 
    function myController($scope, myService) {
       $scope.data = myService.getData();
});

My problem is if I have function in my SERVICE like this

result.getData = function() {
   return data;
}

Everything works fine but I need to filter that data before I get it

If I change body like this I get an empty array the problem seems like it is from AngularJS.

If I create static array it works.

result.getData = function() {
   var arr = [];

        angular.forEach(data, function(item, key) {
              // simple filter
              if(item.ID > 10) {
                  return;
              }          
              else {
                  arr.push(item);
              }  
        });

        return arr;
}
4
  • put a debugger statement in the angular.forEach and look in the console what your datas look like on load. Commented Jan 1, 2013 at 17:41
  • i said ... empty array if i put console debug into angular.foreach that ignore foreach .. if i put before or end foreach it working fine Commented Jan 1, 2013 at 17:43
  • i dont know why doing this because if i direct call on change button it filtered fine .. if varify for source return true but in foreach looks like null or something .. that ignoring foreach Commented Jan 1, 2013 at 17:46
  • data is probably an object, not an array... Commented Jan 1, 2013 at 18:13

1 Answer 1

2

The result of "ResourceData.query()" is asynchronous:

When the data is returned from the server then the object is an instance of the resource class... It is important to realize that invoking a $resource object method immediately returns an empty reference (object or array depending on isArray). Once the data is returned from the server the existing reference is populated with the actual data.

When the page first loads, I'm guessing that the controller runs before the data is returned from the server, so the first time getData() is called, data is probably not yet populated with any data, so you get an empty array.

One solution to your problem would be to filter the data in the view/HTML with an Angular filter, rather than in the service.

<div ng-repeat="item in data | filter:myFilter">
   {{item.ID}}: {{item...}}
</div>

Then in your controller:

$scope.myFilter = function(item) {
   return item.ID <= 10;
}
Sign up to request clarification or add additional context in comments.

1 Comment

thank you a lot for your help... i create filter in my controller and working perfect!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.