在url /user/:userId
中,userId
部分是占位符。您告诉AngularJS将其替换为实际的用户标识以获取真实的URL。
在第二部分中,如果您编写{userId : '@id'}
,您告诉AngularJS,如果未指定userId
,则应使用该对象的id
属性。
我会尝试用几个例子来说明:
如果你
var user = User.get({userId : 123});
您已明确指定一个值userId
所以角将用它来创建URL /user/123/
。但是,如果你做
var user = User();
user.id = 123;
user.get()
在这种情况下,你没有明确指定的userId
的值,所以角将使用user.id
的价值userId
,并重新创建网址/user/123/
。
与rajkamal提及类似,这对于非GET操作非常有用。一个真实的用例就是你这样做的地方:
// user wants to work with Post 10. So we fetch that
var post = Post.get({postId : 10});
// user works with it, makes some changes
post.body = 'New body';
post.topic = 'New topic';
// user is done, and wants to save. You make a POST call
// without having to specify it's id again
post.save();
如果参数值的前缀是@,那么该参数的值是从数据对象中提取的(对于非GET操作很有用)。 – rajkamal