Angular Resource Update Method $ not found

I follow the main Angular page and I have expanded $ resource to add an update method, for example:

angular.module('user', ['ngResource']). factory('User', function($resource) { var User= $resource('/user/:id', { update: { method: 'PUT' } }); User.prototype.update = function(cb) { return User.update({ // this line throws the error id: this.id }, angular.extend({}, this, { _id: undefined }), cb); }; 

However, the launch:

 $scope.user.update() 

calls the TypeError function : Object h (a) {v (a || {}, this)} does not have the 'update' method

I don’t see what I’m missing right now, any help is appreciated

+4
source share
3 answers

I found the problem actually, I need to pass an empty object to paramDefaults arg:

 var User= $resource('/user/:id', {}, { update: { method: 'PUT' } } 
+9
source

You are using a smaller version of angularjs, so you should use an array notation . Just enter the required services into the array, followed by the function:

 angular.module('user', ['ngResource']). factory('User', ['$resource', function($resource) { var User= $resource('/user/:id', { update: { method: 'PUT' } }); User.prototype.update = function(cb) { return User.update({ id: this.id }, angular.extend({}, this, { _id: undefined }), cb); }); }]); 

Note. The same applies to your directives, controllers, ...

0
source

In resource instances, the method name is prefixed with $.

 $scope.user.$update({}, cb); 
0
source

All Articles