2

I have a RESTFUL API whose GET URL's are

For all : /customers.json

For single: /customers/1.json

angular.module('myApp.services', []).factory('Customer', function($resource) {
  return $resource('api/v1/customers/:id.json', { id:'@customers.id' }, {
    update: {
      method: 'PATCH',



    }
    }, {
    stripTrailingSlashes: false
    });
})

Can anyone tell me how I can separate the two get call URLs, trying to check the docs, but they seem to be down.

LeoG
  • 683
  • 1
  • 8
  • 22

1 Answers1

1

you have query for collection and get for object so something like this:

angular.module('app', ['ngResource'])

.service('Customer', function($resource){
    return $resource('api/v1/customers/:id.json');
 })

.controller('ctrl', function(Customer){
    Customer.query().$promise.then(function success(result){
        console.log(result);
    }, function fail(reason){
        console.log(reason);
    }); 

    Customer.get({id: 1}).$promise.then(function success(result){
        console.log(result);
    }, function fail(reason){
        console.log(reason);
    });
})

;

you can also specify if a method is an array, look at the default $resource methods:

{ 'get':    {method:'GET'},
  'save':   {method:'POST'},
  'query':  {method:'GET', isArray:true},
  'remove': {method:'DELETE'},
  'delete': {method:'DELETE'} };

you can check it out here: http://jsbin.com/cesifo/3/edit?html,js,output

and $resource docs here: https://code.angularjs.org/1.4.8/docs/api/ngResource/service/$resource

Alon Valadji
  • 628
  • 4
  • 8
  • u need to use full url, for some reason so didn't parse it well: `https://code.angularjs.org/1.4.8/docs/api/ngResource/service/$resource` – Alon Valadji Dec 10 '15 at 23:21