首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >.save和$save对天使资源的差异

.save和$save对天使资源的差异
EN

Stack Overflow用户
提问于 2014-02-07 13:17:14
回答 2查看 13.8K关注 0票数 15

我看到了将$savesave都称为$resource (角)的代码。

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-02-07 14:42:29

最好的解释===示例:

代码语言:javascript
复制
// 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操作(由您定义)也有它们的$-prefixed对应项,只要它们是不可见的creating-a-custom-put-request

不,并不是所有的动作都有实例方法版本。在实例上调用GET有什么意义?来自官方ngResource文档:

可以使用以下参数调用类对象或实例对象上的操作方法:

  • 获取“类”操作:Resource.action(参数、成功、错误)
  • 非获取“类”操作:Resource.action(参数、postData、成功、错误)
  • 非获取实例操作:实例.$action(参数、成功、错误)
票数 16
EN

Stack Overflow用户

发布于 2014-02-07 13:22:45

$save是由$resource的一个动作添加的方法。保存可以是一种通过特殊资源进行保存的方法。

所有动作都有$前缀的方法。在这里阅读更多:http://docs.angularjs.org/api/ngResource.$resource

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21628787

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档