Javascript AngularJS $resource GET 中的多个参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30604693/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multiple parameters in AngularJS $resource GET
提问by Sami
'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?
这是使用 findRange REST 服务的正确方法。这个返回 rmaID 从 1 到 4,但是如何从控制器使用它以及服务中的正确语法是什么?
In controller I would like to use it something like that:
在控制器中,我想像这样使用它:
$scope.rmas = rmaService.findRange({id:'0'/'3'});
but this is not working.
但这不起作用。
回答by Satpal
You can override url, Read $resourcedocs
您可以覆盖 url,阅读$resource文档
url – {string} – action specific url override. The url templating is supported just like for the resource-level urls.
url – {string} – 特定于操作的 url 覆盖。就像支持资源级 url 一样,支持 url 模板。
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});
回答by pavelety
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
我们这里有 2 个参数:纬度和经度,它们仅在 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');
});
回答by JTime
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.
顺便说一句,我不知道这是否可行,因为我没有使用 ngResource,但这就是我对工厂服务进行编码的方式。

