谁能帮我弄清楚这件事吗?
var discountResource = $resource(GLOBALS.apiPath + 'discounts/:id');
var discountResponse = discountResource.save($scope.discountForm);这将导致到达/discounts
然而,这会导致发布到/discounts (预期行为)
var discountResource = $resource(GLOBALS.apiPath + 'discounts');
var discountResponse = discountResource.save($scope.discountForm);我对此非常执着,因为我想使用第一个选项,声明占位符。但为了我的生命,我无法让它起作用。
我想要选项1的原因是为了能够在工厂中删除它,并将资源注入到控制器中。基本上,我不想每次需要API交互时都重新声明它。我希望这是有意义的。
发布于 2015-06-29 12:01:04
试试像这样的东西
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变量。
引用文件
如果参数值以@为前缀,则该参数的值将从数据对象上的相应属性中提取(在调用操作方法时提供)。例如,如果defaultParam对象是{ someParam:'@someProp'},那么someParam的值将是data.someProp
更多细节请参见https://docs.angularjs.org/api/ngResource/service/$resource (搜索"paramDefaults")
https://stackoverflow.com/questions/31114860
复制相似问题