How to define path section in custom $ resource actions?

Is it possible to specify the path to the user action $ resource?

I would like to write something like:

angular.module('MyServices', [ 'ngResource' ]).factory('User', [ '$resource', ($resource)-> $resource('/users', {}, { names: { path: '/names', method: 'GET', isArray: true } }) ]) 

Therefore, I can use it like:

 User.names() # GET /users/names 
+7
source share
4 answers

It is not supported directly in the current version of AngularJS, but there is a transfer request , so it is likely that it will be supported in the near future.

Until then, you have 3 options:

1) Play with variables along the way:

 $resource('/users/:names', {}, { names: { params: {names: 'names'}, method: 'GET', isArray: true } }) 

2) Use $ http service instead

3) Try to execute the code from the mentioned PR in the AngularJS file, supplemented by a monkey,

+8
source

Check out the logs in this working plunker (excerpt):

 var app = angular.module('plunker', ['ngResource']) .controller('MyController', function($scope, $resource) { var User = $resource( '/users/:action', {}, { names:{ method:'GET', params:{action:'names'} } } ); $scope.users = User.get(); $scope.names = User.names(); } ); 
+5
source

Less eloquent, but no less effective method:

 var resourceUrl = '/users'; return $resource(resourceUrl, {}, { names: { path: resourceUrl + '/names', method: 'GET', isArray: true } }) 
+5
source

From the Angular documentation: https://docs.angularjs.org/api/ngResource/service/ $ resource you can specify a "url" for your custom action, which will undo the previous one.

 angular.module('MyServices', ['ngResource']).factory('User', ['$resource', ($resource)-> $resource('/users', {}, { names: { url: '/users/names', method: 'GET', isArray: true } })]) 

It works in my project using Angular 1.3.10!

+5
source

All Articles