$ watch'ing用于Angular指令中的数据更改

Evi*_*key 132 javascript angularjs angularjs-directive

如何$watch在操作内部数据时触发Angular指令中的变量(例如,插入或移除数据),但不能为该变量分配新对象?

我当前正在从JSON文件加载一个简单的数据集.我的Angular控制器执行此操作,并定义了一些函数:

App.controller('AppCtrl', function AppCtrl($scope, JsonService) {
    // load the initial data model
    if (!$scope.data) {
        JsonService.getData(function(data) {
            $scope.data = data;
            $scope.records = data.children.length;
        });
    } else {
        console.log("I have data already... " + $scope.data);
    }

    // adds a resource to the 'data' object
    $scope.add = function() {
        $scope.data.children.push({ "name": "!Insert This!" });
    };

    // removes the resource from the 'data' object
    $scope.remove = function(resource) {
        console.log("I'm going to remove this!");
        console.log(resource);
    };

    $scope.highlight = function() {

    };
});
Run Code Online (Sandbox Code Playgroud)

我有一个<button>正确调用该$scope.add函数,并将新对象正确插入到$scope.data集合中.我每次点击"添加"按钮时,我设置的表都会更新.

<table class="table table-striped table-condensed">
  <tbody>
    <tr ng-repeat="child in data.children | filter:search | orderBy:'name'">
      <td><input type="checkbox"></td>
      <td>{{child.name}}</td>
      <td><button class="btn btn-small" ng-click="remove(child)" ng-mouseover="highlight()"><i class="icon-remove-sign"></i> remove</button></td>
    </tr>
  </tbody>
</table>
Run Code Online (Sandbox Code Playgroud)

但是,$scope.data当所有这些发生时,我设置的监视指令不会被触发.

我在HTML中定义我的标签:

<d3-visualization val="data"></d3-visualization>
Run Code Online (Sandbox Code Playgroud)

哪个与以下指令相关联(为问题健全性而修剪):

App.directive('d3Visualization', function() {
    return {
        restrict: 'E',
        scope: {
            val: '='
        },
        link: function(scope, element, attrs) {
            scope.$watch('val', function(newValue, oldValue) {
                if (newValue)
                    console.log("I see a data change!");
            });
        }
    }
});
Run Code Online (Sandbox Code Playgroud)

"I see a data change!"在一开始就收到了消息,但是在我点击"添加"按钮后从未收到过消息.

$watch当我只是从data对象添加/删除对象时,如何触发事件,而不是将全新的数据集分配给data对象?

Liv*_* T. 219

您需要启用深层对象脏检查.默认情况下,angular仅检查您观看的顶级变量的引用.

App.directive('d3Visualization', function() {
    return {
        restrict: 'E',
        scope: {
            val: '='
        },
        link: function(scope, element, attrs) {
            scope.$watch('val', function(newValue, oldValue) {
                if (newValue)
                    console.log("I see a data change!");
            }, true);
        }
    }
});
Run Code Online (Sandbox Code Playgroud)

范围.$ watch函数的第三个参数启用深度脏检查,如果它设置为true.

请注意深度脏检查是昂贵的.因此,如果您只需要观察子数组而不是整个data变量,则直接观察变量.

scope.$watch('val.children', function(newValue, oldValue) {}, true);
Run Code Online (Sandbox Code Playgroud)

版本1.2.x引入了$ watchCollection

Shallow监视对象的属性,并在任何属性发生更改时触发(对于数组,这意味着要观察数组项;对于对象映射,这意味着要查看属性)

scope.$watchCollection('val.children', function(newValue, oldValue) {});
Run Code Online (Sandbox Code Playgroud)