How to define a path section in custom actions of $resource?

All we need is an easy explanation of the problem, so here it is.

Is it possible to specify the path of a custom $resource action ?

I would like to write something like:

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

So I can use it like:

User.names() # GET /users/names

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

It is not directly supported in the current version of AngularJS but there is a pull request open so there is chance that it will be supported in the near future.

Till then you’ve got 3 options:

1) Play with variables in the path:

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

2) Use the $http service instead

3) Try the code from the mentioned PR on the monkey-patched version of AngularJS

Method 2

Check the logs in this working Plunker (excerpt):

var app = angular.module('plunker', ['ngResource'])
    function($scope, $resource) {
      var User = $resource(

      $scope.users = User.get();
      $scope.names = User.names();

Method 3

A less eloquent, but equally effective method:

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

Method 4

From Angular documentation:$resource
you can specify ‘url’ for your custom action which will override 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!

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply