【发布时间】:2013-06-18 18:31:44
【问题描述】:
我有一个具有自定义更新方法的资源:
angular.module('user.resources', ['ngResource']).
factory('User', function($resource) {
var User = $resource('/user/:id', {}, {
update: {
method: 'PUT'
}
});
User.prototype.update = function(cb) {
console.log('foo');
return User.update({
id: this._id
}, angular.extend({}, this, {
_id: undefined
}), cb);
};
我正在通过范围将此资源传递给自定义指令:
directive('avatarUpload', function($http) {
return {
restrict: 'E',
scope: {
model: '='
}, ...
我在 btn 点击时调用指令控制器中的更新方法:
$scope.model.update(function() {
console.log('bar');
});
让我困惑的行为是第一次点击按钮打印'foo'而不是'bar',第二次点击它打印'bar',然后'foo'。任何更多的点击总是打印 'bar' 然后 'foo'。
PUT 请求仅在第二次和之后的点击时触发,从不会从第一次触发。
注意:我一直在控制器中使用该资源更新方法,直到尝试从指令中调用它。我正在使用角度 1.1.4 我执行此资源传递是因为我希望该指令适用于不同类型的资源。
【问题讨论】:
标签: angularjs angularjs-directive angularjs-scope