$resource url 中的查询字符串 [英] query string in $resource url
问题描述
由于运行经典 ASP 的服务器的限制,我的服务必须使用查询字符串:
my service has to use a query string due to limitations on the server that runs classic ASP:
angular
.module('myServices', ['ng', 'ngResource'])
.factory('Item', ['$resource',
function ($resource) {
return $resource('/api/?p=item/:id');
}]);
我想向它添加额外的查询字符串参数:
and I want to add extra query string parameters to it:
Item.query({test: 123}, on_success, on_error);
但结果网址是
/api/?p=item?test=123
显然有一个错误,但如何解决它?
apparently there is a bug, but how to get around it?
在 https://github.com/angular/angular.js/issues/1511
推荐答案
您可以使用资源参数.如果您没有在路径中指定占位符,它们将自动转换为查询字符串参数.像这样:
You can use resource parameters. If you haven't specified placeholders in the path, they would automatically be converted into query string params. Like that:
angular
.module('myServices', ['ng', 'ngResource'])
.factory('Item', [
'$resource',
function ($resource) {
return $resource('/api');
}]);
Item.query({p: 'item/1'});
这将导致对 /api?p=item/1
的请求.
This would result in a request to /api?p=item/1
.
附言
我想你已经知道了,但你不喜欢它.但我仍然认为这是您的情况的正确方法.考虑到您正在处理该后端的糟糕 API 设计,您可以将 AngularJS 资源与另一个为您执行此操作的服务包装在一起.
I suppose you already know that, but you don't like it. But I still think this is the correct way in your case. Considering the bad API design you are dealing with that back-end you could wrap the AngularJS resources with another service which does this for you.
这篇关于$resource url 中的查询字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!