How to properly handle resources in AngularJS 1.2?

I updated my AngularJS from 1.0.7 to 1.2rc1 yesterday to check out the new features. Everything is working fine, but I am encountering some problems with ngResource. Using these factory, I get dummy json file data:

angular.module('RESTServices', ['ngResource']) .factory('Customer', function($resource){ return $resource(base+'customer.json', {}, { 'query' : { method: 'GET', isArray: true, cache: caching } }); }); 

I insert a RESTService into the controller and use it like:

 Customer.query({}, function(response){ $scope.customers = response; }); 

On the one hand, they represent an array of clients in $ scope.customers, and for others, additional objects ($ promise and $ are allowed) in the response. A screenshot of the console shows the permission object.

enter image description here

And if I want to repeat the answer, the iterator will go around 200 clients and $ prom and $ resolved, and I get errors because my code does not know how to handle these objects. In version 1.0.7, the response had a total of 200 clients.

Something is wrong? Do I have to check every item during iteration, if not allowed or allowed? The documentation at this point is not entirely clear.

+4
source share
1 answer

The problem was how I iterate over the answer.

I changed

 for(item in items){ for(attr in attributes){ if(items[item][attributes[attr]].toLowerCase().indexOf(filterString) != -1){ matchedItems.push(items[item]); break; } } } 

to

 for(var i = 0; i < items.length; i++){ for(attr in attributes){ if(items[i][attributes[attr]].toLowerCase().indexOf(filterString) != -1){ matchedItems.push(items[i]); break; } } } 

and everything is working fine. AngularJS takes care of the correct length value, so I can use it for for-loop instead of iterating over all values ​​(including all values ​​and $ promised and $ allowed) in the answer.

+1
source

Source: https://habr.com/ru/post/1500662/


All Articles