我见过两者都调用的代码$save
and save
到角度的 $resource。
有什么区别以及什么时候使用?
最好的解释===
例子 :
// by writing '{ id: '@id' }' we want the id to be taken from 'id' parameter in request data, hence the '@' sign. Note that this mechanism is available for non-GET RQs only:
var Notes = $resource('/notes/:id', { id: '@id' });
var noteId = "my_note1";
// below we specify 'id' explicitly - has to be done for GET RQ:
// operations on our note are done inside callback function, just to make sure that the note is resolved:
var note = Notes.get({ id: noteId }, function () {
// let's make some changes:
note.topic = "A brand new topic here!";
// save using $resource "static" action (aka "class" action). 'id' is taken from data object:
Notes.save(note);
// We can overwrite 'id' just like this:
Notes.save({ id: "some_other_noteId" }, note);
// even more changes:
note.body = "Blah blah blah, new boring body is here";
// this time save using instance action. Again: 'id' is taken from data object:
note.$save();
// changing id with instance action? there you go:
note.$save({ id: "yet_another_noteId" });
// Naturally, we could just:
note.id = "OMG_how_many_of_those_noteIds_has_he_left";
Notes.save(note);
// ... and with instance action:
note.id = "OK_he_wins";
note.$save();
});
甚至定制$resource
行动(由您定义)有其$
- 带前缀的对应项,只要它们不是 GET - 请参阅http://docs.angularjs.org/api/ngResource.$resource#example_creating-a-custom-put-request http://docs.angularjs.org/api/ngResource.%24resource#example_creating-a-custom-put-request.
不,并非所有操作都有实例方法版本。调用的意义是什么GET
在一个实例上?来自官方ngResource
docs:
可以使用以下参数调用类对象或实例对象上的操作方法:
- HTTP GET“类”操作:Resource.action([参数], [成功], [错误])
- 非 GET“类”操作:Resource.action([参数], postData, [成功], [错误])
- 非 GET 实例操作:instance.$action([参数], [成功], [错误])
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)