var User = $resource(
'/s:userId/param:otherid',
{
userId: '@id',
otherid:'@ids'
}
);
User.get({
id: '2',
ids: '2'
}, function(resp) {
debugger
}, function(err) {
// Handle error here
});我要创建请求请求:
/s1/顺1‘或/s2/顺2’
但是在firebug网络中,我看到请求加入/s/param?id=2&ids=2。
帮帮我。
发布于 2014-09-21 09:55:03
您误解了$resource url和动词表达式的概念。如$resource documentation所述
参数对象中的每个键值首先绑定到url模板(如果存在),然后将任何多余的键追加到url搜索查询之后。 给定一个模板/path/:谓词和参数{谓词:‘问候’,敬语:‘Hello’}会导致URL /path/greet?salutation=Hello。
由于您的url有以下谓词::userId、:otherid,所以您的request对象应该如下所示:
User.get({
userId: '2',
otherid: '2'
}, function(resp) {
debugger
}, function(err) {
// Handle error here
});另一个误解是使用@表示法,文档指出:
如果参数值以@为前缀,则该参数的值将从数据对象上的相应属性中提取(在调用操作方法时提供)。例如,如果defaultParam对象是{ someParam:'@someProp'},那么someParam的值将是data.someProp。
@表示法只适用于实例操作方法($get、$save、$query等)。请阅读下面代码中提供的评论:
// Sends GET /s2/param2
User.get({
userId: '2',
otherid: '2'
}, function(resp) {
// Let's assume the resp returns {id: 2, ids: 2}
// Then the request below will use you're @ notation (@id, @ids) as a substitute
// to the verbs defined in your url (:userId, :otherid) respectively.
// Sends POST /s2/param2
resp.$save();
}, function(err) {
// Handle error here
});发布于 2014-09-21 08:53:09
从文档:https://docs.angularjs.org/api/ngResource/service/$resource中,您只能使用后缀,所以不使用s或param前缀。
您可以尝试只使用:sid和:paramid捕获参数,然后在User构造函数中修剪s和param部分。
https://stackoverflow.com/questions/25957435
复制相似问题