AngularJS $resource GET 中的多个参数

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

这是使用 findRange REST 服务的正确方法。这个 returns 从 1 到 4 的 rmaID,但我如何从控制器使用它以及服务中的正确语法是什么?

在控制器中,我想像这样使用它:

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

但这不起作用。

您可以覆盖 url,阅读 $resource 文档

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

在资源声明中

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

在控制器中

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

尝试在控制器中使用它更改您的服务,如下所示:

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

我不知道这是否可行,顺便说一句,因为我没有使用 ngResource,但这就是我编写工厂服务的方式。

我更喜欢定义参数的更短方式。这是一个完整的例子。

我们这里有 2 个参数 :latitude 和 :longitude 它们只在 URL 中定义。方法 get 已由 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');
  });