角资源REST没有方法'$保存“方法、资源、REST

2023-09-13 03:43:18 作者:满目敷衍

我想使用 $资源与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();

我问了一下 @ 签署前一阵子同样的问题:"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.

 
精彩推荐