gpt4 book ai didi

angularjs - .save 和 $save 到 angularjs 中资源的区别

转载 作者:行者123 更新时间:2023-12-03 20:38:56 28 4
gpt4 key购买 nike

我见过两个都调用 $save 的代码和 save到 $resource 的 Angular 。

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

最佳答案

最佳解释===例子 :

// 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 Action (由您定义)有它们的 $ - 前缀对应物,只要它们不是 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, [success], [error])
  • non-GET instance actions: instance.$action([parameters], [success], [error])

关于angularjs - .save 和 $save 到 angularjs 中资源的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21628787/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com