.save和$区别保存angularjs资源 [英] Difference between .save and $save to resource in angularjs

查看:221
本文介绍了.save和$区别保存angularjs资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过code这两个电话 $保存保存来在一个角$资源

I've seen code that both calls $save and save to a $resource in 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();
});

甚至是自定义 $资源动作(由您定义)有自己的 $ - prefixed同行,如只要他们不GET - 见<一href=\"http://docs.angularjs.org/api/ngResource.%24resource#example_creating-a-custom-put-request\">http://docs.angularjs.org/api/ngResource.$resource#example_creating-a-custom-put-request.

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

Even custom $resource actions (defined by you) have their $-prefixed counterparts, as long as they're non-GET - see http://docs.angularjs.org/api/ngResource.$resource#example_creating-a-custom-put-request.
And no, not all actions have instance method version. What would be the point of invoking GET on an instance? From official ngResource docs:

类对象或实例对象上的操作方法,可以用下面的参数来调用

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


      
  • HTTP GET下课的行动:Resource.action([参数],[成功],[错误])

  •   
  • 非GET下课的行动:Resource.action([参数],POSTDATA,[成功],[错误])

  •   
  • 非获得实例动作:例如$行动([参数],[成功],[错误])

  •   

这篇关于.save和$区别保存angularjs资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆