Regarding the problem in $resource in angular.js.
伊谢尔伦
伊谢尔伦 2017-05-15 17:01:49
0
3
506

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,但是这前前后后到底有什么因果关系呢?
伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(3)
世界只因有你

Thanks for the invitation, although I have never used itngResource.

Then I will answer them in order

"What does /api/entries/:id里,:idmean"

Answer: :id是一个占位符行为,意思是,在你真正发起请求时,该位置会被真正传入的的参数所替换。譬如:/api/entries/9970

"What does the second parameter{id:'@_id'}里,@_idmean?"

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 understand

迷茫

I haven’t used it for a long time$resource了,不过'/api/entries/:id'是请求数据的路由,:id会被后面的{id:'@_id'}中的@_id替代,:idIt’s a routing parameter, it should look like this, I haven’t used it for a long time, I forgot

滿天的星座

Thanks for the invitation.

Actually, I hardly use it. $resource这个东西,与ui-routerSimilar, 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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template