简体   繁体   中英

Multiple parameters in AngularJS $resource GET

'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.

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});

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');
  });

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM