10
'use strict';
angular.module('rmaServices', ['ngResource'])
    .factory('rmaService', ['$resource',
        function ($resource) {
            return $resource(
                   '/RMAServerMav/webresources/com.pako.entity.rma/:id',

                    {},
                   {
                      delete: { method: 'DELETE', params: {id: '@rmaId'}}, 
                      update: { method: 'PUT', params: {id: '@rmaId'}},
                      //RMAServerMav/webresources/com.pako.entity.rma/0/3
                      findRange:{method: 'GET', params:{id:'@rmaId'/'@rmaId'}}
                    });
        }]);

RMAServerMav/webresources/com.pako.entity.rma/0/3

This is correct way to use findRange REST service. This one returns the rmaID from 1 to 4, but how can I use this from controller and what is the correct syntax in service?

In controller I would like to use it something like that:

$scope.rmas = rmaService.findRange({id:'0'/'3'});

but this is not working.

Sami
  • 2,311
  • 13
  • 46
  • 80

3 Answers3

24

You can override url, Read $resource docs

url – {string} – action specific url override. The url templating is supported just like for the resource-level urls.

In resource declaration

findRange:{ 
    url: '/RMAServerMav/webresources/com.pako.entity.rma/:id/:to', 
    method: 'GET', 
    params:{ 
        id:'@id', 
        to: '@to'
    }
}

In Controller

$scope.rmas = rmaService.findRange({id:0, to: 3});
Satpal
  • 132,252
  • 13
  • 159
  • 168
  • Thanks, that was it. I really don't like Google document about AngularJS. Examples are too simply and documentation is....not for me or they are too mixed. Basically I tried this but I didn't change the url. How about if you have to get from two tables (joins etc)? Is it possible? – Sami Jun 02 '15 at 21:17
0

I would prefer shorter way of defining parameters. Here is a complete example.

We have here 2 params :latitude and :longitude they defined only in the URL. Method get is already defined by ngResource

angular.module('myApp', ['ngResource'])
  .controller('myCtrl', function (ReverseGeocoderResource) {
    ReverseGeocoderResource.get({longitude: 30.34, latitude: 59.97}).$promise.then(function (data) {
      console.log(data.address.road + ' ' + data.address.house_number);
    })
  })
  .factory('ReverseGeocoderResource', function ($resource) {
    return $resource('https://nominatim.openstreetmap.org/reverse?format=json&lat=:latitude&lon=:longitude&zoom=18&addressdetails=1&accept-language=ru');
  });
pavelety
  • 746
  • 6
  • 8
-1

Try changing your service using it in the controller like this:

'use strict';
angular.module('rmaServices', ['ngResource'])
    .factory('rmaService', ['$resource',
        function ($resource) {

          var service ={}

          service.rma = function(){ // name it whatever you want

                return $resource(
                   '/RMAServerMav/webresources/com.pako.entity.rma/:id',

                    {},
                   {
                      delete: { method: 'DELETE', params: {id: '@rmaId'}}, 
                      update: { method: 'PUT', params: {id: '@rmaId'}},
                      //RMAServerMav/webresources/com.pako.entity.rma/0/3
                      findRange:{method: 'GET', params:{id:'@rmaId'/'@rmaId'}}
                    });
          };
          return service;
}]);


//in controller
rmaService.rma()
  .then(function(resource){
    $scope.rmas = resource.$findRange({id:'0'/'3'});
  });

I have no idea if this will work BTW, because I have no use ngResource but that is how I code my factory services.

JTime
  • 29
  • 8