在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