AngularJS中.save和$save到资源的区别

2024-04-21

我见过两者都调用的代码$save and save到角度的 $resource。

有什么区别以及什么时候使用?


最好的解释===例子 :

// 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行动(由您定义)有其$- 带前缀的对应项,只要它们不是 GET - 请参阅http://docs.angularjs.org/api/ngResource.$resource#example_creating-a-custom-put-request http://docs.angularjs.org/api/ngResource.%24resource#example_creating-a-custom-put-request.
不,并非所有操作都有实例方法版本。调用的意义是什么GET在一个实例上?来自官方ngResource docs:

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

  • HTTP GET“类”操作:Resource.action([参数], [成功], [错误])
  • 非 GET“类”操作:Resource.action([参数], postData, [成功], [错误])
  • 非 GET 实例操作:instance.$action([参数], [成功], [错误])
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

AngularJS中.save和$save到资源的区别 的相关文章

  • 在 AngularJS 中设置应用程序范围的 HTTP 标头

    有没有办法设置 httpProvider标题之外angular module myApp config 登录用户后 我从服务器获取身份验证令牌 我需要将其作为 HTTP 标头添加到所有后续请求中 您可以使用角度的默认标题1 0 x http
  • 在 Angular 单元测试中应该如何处理运行块?

    我的理解是 当您在 Angular 单元测试中加载模块时 run块被调用 我认为如果你正在测试一个组件 你不会想同时测试run块 因为unit测试应该只是测试一个unit 真的吗 如果是的话有什么办法可以防止run阻止运行 我的研究让我认为
  • 在 angularjs 模块初始化期间有条件地注入依赖项

    我有一个角度模块 我想有条件地将依赖项注入其中 IE var myapp angular module myapp ngRoute myappcontroller ngGrid I want to include ngGrid only i
  • 如何让 Angular-Flask 应用加载 html 部分?

    我试图让我的 Angular Flask 应用程序在基本 html 文件中渲染部分 HTML 文件 应用程序加载基本 html 窗口标题和页脚加载 但 ng view 没有加载任何内容 也许我到局部的角度路由不正确 文件结构 gt flas
  • Webpack 和 Angular HTML 图像加载

    我一直对 webpack 和 Angular 感到头疼 这可能有一个简单的答案 但我无法弄清楚 我已经阅读了堆栈溢出中关于这个主题的几乎所有答案 但都无济于事 我有一个像这样的 html 页面 还有其他包含图像的模板 img
  • Angular 1.6 和多个模块:在模块和组件之间共享全局变量

    我正在使用 Angular 1 6 7 我在我的应用程序中创建了多个模块 如何将父模块 myApp 中定义的常量 例如 rootURL 传递给子模块 childApp 更具体地说 我需要将 rootURL 的值分配给 childApp 组件
  • 使用 ng-options 在 AngularJS 中使用 JSON 填充 select

    编辑 我的代码实际上确实有效 我只是一个有不相关问题的白痴 感谢大家的意见 所以我有一个 JSON 对象数组 格式如下 id id1 text text1 id id2 text text2 我想使用这些填充 AngularJS 选择字段
  • 当最初在范围内设置值时,日期选择器弹出格式不起作用

    我正在使用 Angular UI 引导日期选择器弹出窗口 并在 Plunker 上使用此自定义指令 http plnkr co edit 053VJYm1MpZUiKwFTfrT p preview http plnkr co edit 0
  • Angular js 服务器端过滤器和分页

    我有几个输入字段 我们可以使用带有搜索名称的输入字段作为示例 当有人在该字段中输入内容时 我希望能够过滤对象的结果 但我想做过滤器服务器端而不是客户端 我有一个包含大量记录的数据库 因此我不想返回所有记录并在客户端进行过滤 这确实会减慢速度
  • 从 JSON 文件注入的编译指令 AngularJS

    希望有人能帮助我应对这一挑战 我使用以下命令从服务器请求 JSON 数据 http get 来自服务器的数据返回一个对象 对象中的一个值包含 HTML 标记 该标记使用以下方式注入到页面中 div div 在标记内 有一个名为的自定义指令
  • Jasmine-jQuery loadFixtures 未定义

    我对整个茉莉花的事情仍然很陌生 在过去的几个小时里我陷入了这个问题 我尝试使用 loadFixture 加载外部夹具文件 我使用 Jasmine 2 0 0 和 Jasmine jQuery 2 0 5 ReferenceError loa
  • 如何使用 $resource 获取状态代码?

    我提出请求的工厂在这里 angular module myapp factory testResponse http resource AppConfig routeParams rootScope function http resour
  • AngularJS - 获取已定义路由的列表 - $routeProvider

    我正在尝试实施named routes 所以我不必写整个路径 经常改变 我想我可以编写一个服务来返回定义的路由列表和一个将对象转换为路由的过滤器 使用示例如下所示 a Click here a 假设我已将 name detail 添加到我的
  • AngularJS - 关闭模态窗口

    我的内容包括 bootstrap css getbootstrap com 2 3 2 angular ui bootstrap tpls 0 10 0 min js from angular ui github io bootstrap
  • Angular UI select:从远程服务获取数据

    我正在使用角度用户界面选择 https github com angular ui ui select https github com angular ui ui select 我查看了演示的可用位置这个笨蛋 http plnkr co
  • Angular 模态对话框最佳实践

    与不具有动态内容的对话框相比 创建具有动态内容的模式对话框的最佳实践是什么 例如 我们有一些模式表单接受表单元素列表 并具有提交 取消功能 此外 还有一些模式对话框仅显示确认 确定类型的操作 我见过很多人说对话框应该是传递到控制器的服务 但
  • 模板中带有 ng-if 的 angularjs 指令

    我正在构建一个在模板内使用 ng if 的指令 奇怪的是 提供给链接函数的元素没有扩展ng if代码 它只是ng if的注释行 经过一番尝试 我发现通过将链接代码包装在 timeout 中似乎可以使其正常工作 但我想知道这是否不是正确的处理
  • 为什么我无法将 $location 注入到我的 config() 中?

    为什么这会给我一个错误 angular module app config function routeProvider locationProvider httpProvider location 未捕获的错误 未知的提供商 来自应用程序
  • 以角度访问窗口 TemplateUrl 内的范围

    我的模式有一个 windowTemplateUrl 如下 div class modal fade div class modal dialog div class modal content square btn div div div
  • AngularJS 在指令运行之前通过 AJAX 检索数据

    我正在使用 AngularUIuiMap http angular ui github com directives map实例化谷歌地图的指令 uiMap 指令非常适合处理硬编码数据 mapOptions and myMarkers 但是

随机推荐