Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

angularjs

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
like image 680
Romain Tribes Avatar asked Feb 25 '13 21:02

Romain Tribes


2 Answers

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

like image 90
pkozlowski.opensource Avatar answered Nov 07 '22 15:11

pkozlowski.opensource


Check 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();
    }  
  );
like image 35
Stewie Avatar answered Nov 07 '22 14:11

Stewie