角资源REST没有方法'$保存“ [英] Angular resource REST has no method '$save'

查看:94
本文介绍了角资源REST没有方法'$保存“的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 $资源与REST API交互,但是打电话时保存方法,我得到有没有方法'$保存错误。我的code是由 AngularJS $找到了答案启发资源的RESTful例子

I would like to interact with a REST API using $resource but when calling the save method I get has no method '$save' error. My code is inspired by the answer found on AngularJS $resource RESTful example

myapp.factory('Monitoring', function($resource) {
   return $resource('http://localhost:8080/wepapp/network/v1/cronjobs/:id', { id: '@id' } );
});

Q1:什么是的@ 目的{ID:'@id'} ?我发现它在大多数的例子。

Q1: What is the purpose of @ in { id: '@id' }? I found it in most of the examples.

myapp.factory('MonitoringCRUDControllerService', ['Monitoring', function(Monitoring) {
    return {
        create: function(id, command, schedule) {
            console.log("create");
            console.log(command);
            console.log(schedule);
            Monitoring.id = id;
            Monitoring.command = command;
            Monitoring.schedule = schedule;
            console.log(Monitoring);
            Monitoring.$save();
        }
    }
}]);

监控对象是否正确注入:

function Resource(value){
        copy(value || {}, this);
      } 

调用 $保存失败,错误没有方法'$保存

Q2:什么 $ 前的目的保存

Q2: What the purpose of $ before save?

Q3:我在想什么,使保存方法的工作。

Q3: What am I missing to make the save method work?

推荐答案

您需要创建监控类这样的(未测试)的一个实例:

You need to create an instance of your Monitoring class like this (not tested):

var m = new Monitoring({id:id});
m.command = command;
m.schedule = schedule;
m.$save();

文档( http://docs.angularjs.org/api/ngResource.$resource )也有类似的例子:

var newCard = new CreditCard({number:'0123'});
newCard.name = "Mike Smith";
newCard.$save();

我问了一下 @ 签署前一阵子同样的问题:<一href=\"http://stackoverflow.com/questions/13313971/at-sign-in-parameter-names-in-resource-definition\">"at"登录在资源定义的参数名称。基本上 @ 标志意味着该值将从对象的属性被读取。或为同一文件说:

I asked the same question about the @ sign a while ago: "at" sign in parameter names in resource definition. Basically @ sign means that the value will be read from the object's property. Or as the same documentation says:

如果该参数值是与@ pfixed则该值$ P $
  参数从数据对象中提取(有用非GET
  操作)。

If the parameter value is prefixed with @ then the value of that parameter is extracted from the data object (useful for non-GET operations).

$ 没有任何特殊字符,它仅仅是一个方法名称的一部分。

The $ is not any special character, it is just a part of the method name.

这篇关于角资源REST没有方法'$保存“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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