加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-17 07:44:03 所属栏目:安全 来源:网络整理
导读:我看到代码都调用$save并保存到$资源的角度. 有什么区别,什么时候使用? 最佳解释===示例: // 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 fo
我看到代码都调用$save并保存到$资源的角度.

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

最佳解释===示例:
// 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();
});

即使自定义$资源操作(由您定义)也可以使用$-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])

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读