我有一個自定義更新方法的資源:
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');
});
行爲whic h困惑我atm是,第一次打印'foo'而不是'bar'時點擊按鈕,第二次打印'bar'然後點擊'foo'。任何更多的點擊總是打印'酒吧',然後'富'。
PUT請求僅在第二次點擊時觸發,而後一次點擊則從第一次觸發。
注意:我一直在控制器中使用資源更新方法,直到試圖從指令調用它。我正在使用角1.1.4 我做這個資源傳遞,因爲我想指令工作在不同類型的資源。
真棒回答,歡呼! –