Multiple parameters in AngularJS $resource GET
Asked Answered
I

3

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.

Illfounded answered 2/6, 2015 at 19:15 Comment(0)
D
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});
Dearborn answered 2/6, 2015 at 19:19 Comment(1)
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?Illfounded
I
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');
  });
Isonomy answered 9/5, 2019 at 8:38 Comment(0)
H
-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.

Homochromatic answered 2/6, 2015 at 20:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.