I'm new in Angularjs and got a doubt. I'm using $resource to consume a Rest Web Service this way:
providersApp.factory('providersSrvc', function ($resource) {
return {
getData: function () {
return $resource('http://devfz.azurewebsites.net/api/providers', {}, {
query: { method: 'GET' }
});
}
}
});
Here is my controller:
providersApp.controller('ProvidersController',
function ProvidersController($scope, providersSrvc) {
$scope.providers = providersSrvc.getData().query();
});
This Rest URL is returning a JSON object with an array inside it (correct me if I am wrong):
{"$id":"1","$values":[{"$id":"2","Id":1,"Name":"Diagnose","Category":null,"Address":null,"Services":null},{"$id":"3","Id":2,"Name":"Hospital São lucas","Category":null,"Address":null,"Services":null},{"$id":"4","Id":3,"Name":"Hospital Primavera","Category":null,"Address":null,"Services":null}]}
I've found a way to iterate it like this:
provider in providers.$values
My question is: Is this considered a good way to iterate it? All examples I found in internet seems to return JSON array, not an array inside a JSON object like mine. Is anything wrong with my Restful JSON return? Is there a better way to do all this?
Thanks so much for any help!
'query': {method:'GET', isArray:true},.isArray, but not much.$idand$valuesfield... so there's no way to get an array from it directly. What you have for parsing is good but I'd recommend (if you plan on using $values as an array in your controller/template) assigning it to a $scope variable in a callback.