Just like the following code:
angular.module('myApp').factory('Entry', function($resource){
return $resource('/api/entries/:id', {id:'@_id'},{
update: {
method: 'PUT'
}
});
});
这里面的/api/entries/:id这个:id是什么意思啊?{id:'@_id'}还有这个@_id又表示什么意思呢?为什么前面多加了一个_了,我知道好像$resource在保存数据的时候会自动增加一个属性就是_id,但是这前前后后到底有什么因果关系呢?
Thanks for the invitation, although I have never used it
ngResource
.Then I will answer them in order
"What does
/api/entries/:id
里,:id
mean"Answer:
:id
是一个占位符行为,意思是,在你真正发起请求时,该位置会被真正传入的的参数所替换。譬如:/api/entries/9970
"What does the second parameter
{id:'@_id'}
里,@_id
mean?"Answer: The second parameter is the default parameter. If it starts with
@
开头,则表示真正请求时,会从你传入的对象中找@
后面的的字段,这里就是从你传入的对象中找_id
字段对应的值,并替换URL里的:id
, it forms a real URL.Finally, although the documentation of
ngResource
is not very well written, it contains what I mentioned. It is recommended to read the document carefully. If you don’t understand something, do more experiments and compare the results to understandI haven’t used it for a long time
$resource
了,不过'/api/entries/:id'
是请求数据的路由,:id
会被后面的{id:'@_id'}
中的@_id
替代,:id
It’s a routing parameter, it should look like this, I haven’t used it for a long time, I forgotThanks for the invitation.
Actually, I hardly use it.
$resource
这个东西,与ui-router
Similar, in fact, to put it bluntly, it is a problem of passing parameters when the route is changed, but the form is different. @leftstick said it in detail, I don’t need to explain it anymore.