By default, the $resource.query()
is set up to expect an array of objects that become $resource
objects. To accommodate paging in a nice, restful way, I have my GET /api/widgets
endpoint set up to return the following object:
{
currentPage: 1,
perPage: 20,
totalItems: 10039,
items: [{...}, {...}, {...}]
}
Is there a way to make it so that angular will know that the items
property is the array of items to be $resource
objects?
You need to specify your own custom action.
I imagine your code looks something like this:
factory('Widget', function($resource) {
return $resource('/api/widgets');
});
Change it to this:
factory('Widget', function($resource) {
return $resource(/api/widgets, null, {
query: {
method: 'GET',
isArray: true,
transformResponse: function(data) {
return angular.fromJson(data).items;
}
}
});
});
the easy was is to use $resouce.get
, if you wan to use query you can override that behaivor.
$resource('/notes/:id', null,
{
'query': {method:'GET', isArray:false}
});
more info https://docs.angularjs.org/api/ngResource/service/$resource
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With