【发布时间】:2015-10-09 09:31:28
【问题描述】:
我有一个带有以下代码的指令:
(function(window, angular, undefined) {'use strict';
angular.module('widgets.avatarEditor', [])
.controller('AvatarEditorController', [
'$scope',
'$attrs',
'$timeout',
function($scope, $attrs, $timeout) {
}
])
.directive('avatarEditor', function() {
return {
restrict: 'E',
scope: {
originalImage: '=originalImage'
},
controller: 'AvatarEditorController',
templateUrl: '/config/static/partials/widgets/avatareditor.html',
link: function (scope, elm, attr) {
scope.watch('originalImage',function (newValue, oldValue, scope) {
console.log(newValue);
console.log(oldValue);
console.log(scope);
});
}
};
});
})(window, window.angular);
该指令在应用程序中使用:
部分:
<avatar-editor original-image="currentPicture"></avatar-editor>
控制器:
$scope.currentPicture = 'b';
$scope.avatars = AvatarsResource.get({}, function() {
$scope.currentPicture = 'bar';
});
没有scope.watch('originalImage',function (newValue, oldValue, scope)... 代码一切正常,但是当我添加手表代码时,突然“originalImage”模型未定义。
奇怪的是,watch 事件被正确触发了。
有没有人知道,是什么导致了这种行为?
编辑: 我发现watch的监听函数的参数有些奇怪: newValue 包含“originalImage” oldValue 包含“b”(真正的旧值) context 包含“bar”(真正的新值)
【问题讨论】:
-
你能为此做一个 jsFiddle 的例子吗?看到这种情况在行动中发生会有所帮助。
标签: angularjs angularjs-directive