scope.watch将观察到的模型设置为Angular中的“undefined”

时间:2015-10-09 09:31:29

标签: angularjs angularjs-directive

我有一个带有以下代码的指令:

(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的监听器功能的参数有些奇怪: newValue包含“originalImage” oldValue包含“b”(真正的旧值) 上下文包含“bar”(真正的新值)

1 个答案:

答案 0 :(得分:0)

好的,我感觉很蠢,现在......

scope.watch('!= scope.$watch('