简体   繁体   English

angular指令不听外部范围的变化

[英]angular directive not listening to changes in outer scope

I'm currently trying to get my directive to listen to changes happening to a specific variable in the parent scope. 我目前正试图让我的指令听取父作用域中特定变量发生的变化。 For that purpose I came up with the following code which runs fine the first time the controller gets loaded but is not picking up the change to the variable gridCells in the second part running in the $timeout. 为此,我提出了以下代码,该代码在第一次加载控制器时运行良好,但是没有在$ timeout中运行的第二部分中获取对变量gridCells的更改。

Anyone able to give me a hint what I'm doing wrong here? 有人能给我一个提示我在这里做错了吗?

Controller: 控制器:

$scope.gridCells = [];
$scope.gridCells.push({value: '1'});
$scope.gridCells.push({value: '2'});
$scope.gridCells.push({value: '1'});
$scope.gridCells.push({value: '2'});

// this change is not reflected in the directive
$timeout(function() {
  console.log('push');
  $scope.gridCells.push({value: '2'});
  $scope.gridCells.push({value: '1'});
}, 4000);

HTML: HTML:

<my-directive cells=gridCells></my-directive>

Directive: 指示:

angular.module('myApp').directive('myDirective', [ function() {
  return {
    restrict: 'E',
    scope: {
      cells: '='
    },
    link: function (scope, element, attrs) {

      scope.gridCells = [];

      scope.$watch(attrs.cells, function(newValue, oldValue) {

        console.log('new: ' + newValue);
        console.log('old: ' + oldValue);

        for(var i = 0; i < scope.cells.length; i++) {
          // populate my scope.gridCells variable with some of the content in the UPDATED cells variable
          if (scope.cells[i].value === '1') {
            gridCells.push(scope.cells[i]);
          }

        }

      });

    },
    template: '{{gridCells.length}}'
  };
}]);

You need to use $watchCollection instead of $watch or do a deepWatch ( scope.$watch(prop, func, true) ) in order to be able to track the changes in the array that has been 2-way bound. 您需要使用$watchCollection而不是$watch或执行deepWatch( scope.$watch(prop, func, true) ),以便能够跟踪已经双向绑定的数组中的更改。 It is also better to watch scope property cells instead of attribute cells 观察范围属性cells而不是属性cells也更好

scope.$watchCollection('cells', function() {
 //probably you want to reset gridCells here?
  //scope.gridCells = [];
  for(var i = 0; i < scope.cells.length; i++) {
     if (scope.cells[i].value === '1') {
      scope.gridCells.push(scope.cells[i]);
   }
  }
});

Plnkr Plnkr

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular - 监听由控制器执行的指令中的绑定更改 - Angular - listening to binding changes in directive executed by controller Angular指令范围未反映控制器范围的更改 - Angular directive scope not reflecting changes in controller scope 自定义角度指令:如何监视范围更改 - Custom angular directive : how to watch for scope changes 来自外部范围的 Angular 指令和方法不起作用 - Angular directive and method from outer scope don't work AngularJS:带有HTML和角度表达式的指令,使用外部范围“编译”内容 - AngularJS: directive with HTML and angular expressions, “compile” the contents with the outer scope 指令模板主体中的Angular 1.x消耗外部作用域 - Angular 1.x consuming outer scope within directive template body 为什么即使我将范围选项设置为false,从指令内部更改外部范围也需要scope。$ apply()? - Why does changes to the outer scope from within a directive require scope.$apply() even if I am setting scope option to false? Angular指令代码更改范围,但模板显示相同的值 - Angular directive code changes scope, but template shows the same value 角度-对指令控制器范围的更改未反映在视图中 - Angular - changes to directive controller's scope aren't reflected in view 在Angular指令中更新$ scope - Updating $scope in an Angular directive
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM