4

我正在关注 Angular 主页,并扩展了 $resource 以添加如下更新方法:

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);
  };

但是运行:

$scope.user.update()

抛出TypeError: Object function h(a){v(a||{},this)} has no method 'update'

我现在看不到我缺少的东西,任何帮助表示赞赏

4

3 回答 3

9

我发现这个问题实际上我需要为 paramDefaults arg 传递一个空对象:

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

}
于 2013-01-13T18:20:10.923 回答
0

您正在使用缩小的 angularjs 版本,因此您必须使用 to array notation。只需在数组中注入所需的服务,然后是函数:

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);
  });
}]);

注意:这同样适用于您的指令、控制器、...

于 2013-01-11T21:22:07.803 回答
0

在资源实例中,方法名称以 $ 为前缀。

$scope.user.$update({}, cb);
于 2013-01-12T17:49:35.757 回答