我在meanjs中有示例代码,我不明白为什么他们必须设置第三个参数
update: {
method: 'PUT'
}
这是完整的代码:
'use strict';
//Articles service used for communicating with the articles REST endpoints
angular.module('articles').factory('Articles', ['$resource',
function ($resource) {
return $resource('api/articles/:articleId', {
articleId: '@_id'
}, {
update: {
method: 'PUT'
}
});
}
]);
提前致谢。
最佳答案
如果你看一下docs特别是在返回部分下,您将看到$resource
服务将返回:
A resource "class" object with methods for the default set of resource actions optionally extended with custom actions. The default set contains these actions:
{'get': {method:'GET'},
'save': {method:'POST'},
'query': {method:'GET', isArray:true},
'remove': {method:'DELETE'},
'delete': {method:'DELETE'} };
它进一步指出:
The actions save, remove and delete are available on it as methods with the $ prefix.
因此,$save
、$remove
、$delete
可用,但没有$update。这就是示例中的服务具有以下行的原因:
...
'update': { method: 'PUT'},
...
它的目的是扩展这些默认操作集,以便 $update
将可用作对象上的方法,并且它将使用 HTTP PUT 方法,而不是像其他方法那样使用 GET/POST/DELETE .
注意:以上答案摘自previous question I answered但我在这里隔离了您应该关注的部分
关于javascript - 为什么必须设置更新: { method: 'PUT' } in $resource angularjs?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33907503/