ngResource保存()的奇怪行为 [英] ngResource save() strange behaviour
问题描述
有人请阐明这一些轻?
Can someone please shed some light on this?
var discountResource = $resource(GLOBALS.apiPath + 'discounts/:id');
var discountResponse = discountResource.save($scope.discountForm);
这会导致一个GET到/折扣
This results in a GET to /discounts
然而这会导致一个POST /折扣(预期的行为)
However this results in a POST to /discounts (expected behaviour)
var discountResource = $resource(GLOBALS.apiPath + 'discounts');
var discountResponse = discountResource.save($scope.discountForm);
我对此很坚持,因为我想用第一个选项,以声明的占位符。但对我的生活,我不能得到它的工作。
I'm very stuck on this, as I would like to use the first option, with the placeholder declared. But for the life of me I cannot get it to work.
我要选择1的原因是这样我就可以在工厂decalre并注入资源进入我的控制器。基本上我不希望每次我需要一个API互动时间重新声明它。我希望是有道理的。
The reason I want option 1 is so that I can decalre it in a factory and inject the resource into my Controllers. Basically i don't want to redeclare it every time I need an API interaction. I hope that makes sense.
推荐答案
尝试这样的事情
Module.factory("Discount", ["$resource", function ($resource) { return $resource(GLOBALS.apiPath + "discounts/:Id", { Id: "@Id" }, {
somthingCustomIfNeeded: { method: 'POST', url: GLOBALS.apiPath + "something-custom" }
}); }]);
注意 {ID:@Id}
对象吗?它告诉角度如何解决:id变量
Notice the { Id: "@Id" }
object? It tells Angular how to resolve that :Id variable
从文档报价
如果该参数值是与@ pfixed然后该参数的值将从数据对象相应的属性来提取$ P $(设置调用操作方法时)。例如,如果该defaultParam对象是{someParam:'@someProp'}然后someParam的值将是data.someProp
If the parameter value is prefixed with @ then the value for that parameter will be extracted from the corresponding property on the data object (provided when calling an action method). For example, if the defaultParam object is {someParam: '@someProp'} then the value of someParam will be data.someProp
更多细节 https://docs.angularjs.org/api/ngResource/service/ $资源
(搜索paramDefaults)
More details here https://docs.angularjs.org/api/ngResource/service/$resource (Search for "paramDefaults")
这篇关于ngResource保存()的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!