.save和$save之间的差异在angularjs中的资源

前端之家收集整理的这篇文章主要介绍了.save和$save之间的差异在angularjs中的资源前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我看到代码调用$save并保存到$资源的角度.

有什么区别,什么时候使用?

最佳解释===示例:
  1. // 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:
  2. var Notes = $resource('/notes/:id',{ id: '@id' });
  3.  
  4. var noteId = "my_note1";
  5. // below we specify 'id' explicitly - has to be done for GET RQ:
  6. // operations on our note are done inside callback function,just to make sure that the note is resolved:
  7. var note = Notes.get({ id: noteId },function () {
  8.  
  9. // let's make some changes:
  10. note.topic = "A brand new topic here!";
  11.  
  12. // save using $resource "static" action (aka "class" action). 'id' is taken from data object:
  13. Notes.save(note);
  14. // We can overwrite 'id' just like this:
  15. Notes.save({ id: "some_other_noteId" },note);
  16.  
  17. // even more changes:
  18. note.body = "Blah blah blah,new boring body is here";
  19.  
  20. // this time save using instance action. Again: 'id' is taken from data object:
  21. note.$save();
  22. // changing id with instance action? there you go:
  23. note.$save({ id: "yet_another_noteId" });
  24.  
  25. // Naturally,we could just:
  26. note.id = "OMG_how_many_of_those_noteIds_has_he_left";
  27. Notes.save(note);
  28. // ... and with instance action:
  29. note.id = "OK_he_wins";
  30. note.$save();
  31. });

即使自定义$资源操作(由您定义)也可以使用$-prefixed对等方,只要它们不是GET – 请参见http://docs.angularjs.org/api/ngResource.$resource#example_creating-a-custom-put-request.
而不是,并不是所有的动作都有实例方法版本.在实例上调用GET的要点是什么?从官方ngResource文档:

The action methods on the class object or instance object can be invoked with the following parameters:

  • HTTP GET “class” actions: Resource.action([parameters],[success],[error])
  • non-GET “class” actions: Resource.action([parameters],postData,[error])
  • non-GET instance actions: instance.$action([parameters],[error])

猜你在找的Angularjs相关文章