我正在使用ngresource,但很遗憾,我无法以这种方式访问$update
,$remove
和$save
方法。我做错了什么?
angular.module('myApp.services').factory('Entry', function($resource) {
return {
method1: $resource('/api/entries/:id', { id: '@_id' }, {
update: {
method: 'PUT' // this method issues a PUT request
}
}),
method2: $resource('/api/entries2', {}, {
})
});
// not working: Entries is not a function at Scope.$scope.save
var entry = new Entries({});
entry.method1.$update();
entry.method1.$save();
entry.method1.$delete();
另一方面,这有效:
angular.module('myApp.services').factory('Entry', function($resource) {
return $resource('/api/entries/:id', { id: '@_id' }, {
update: {
method: 'PUT' // this method issues a PUT request
}
});
});
var entry = new Entries({});
entry.$update();
entry.$save();
entry.$delete();
答案 0 :(得分:1)
所以你的第二个例子是做$ resource(' http://example.com/resource.json')是正确使用那个结构,而第一个不是。
执行var entry = new Entries({})
后;您在控制器中获得entry
作为工厂实例,该实例具有您为其定义的可用操作。
<强> UPD 强>
您可以在服务中拥有多个资源 - https://stackoverflow.com/a/17163459/405623。在您的示例中,您刚刚错过了模块中的['ngResource']
DI。