深入观察数组在 AngularJS 数据绑定中不起作用

Deep watch on array not working in AngularJS data binding

我正在使用 AngularStrap 的 popover 指令。我有一个对象集合,这些对象被渲染为切换。当点击开关时,示波器上的数据没有改变......我已经尝试了很多不同的方法来做到这一点。

这是弹出窗口的控制器

app.controller('CurvesPopoverCtrl', ['$scope', '$filter', 'AppData',
    function($scope, $filter, AppData) {
        'use strict';

        $scope.popoverCurrencies = AppData.getCurrencies();
        $scope.$watch ('popoverCurrencies', function(val) {
            if(val !== null && val !== undefined){ // only fires when popover opens.
                console.log("toggled", val);
                //AppData.setCurrencies($scope.currencies);
            }
        }, true); // deep watch


    }
]);

这是弹出窗口的模板

<div class="well curves-popover" ng-controller="CurvesPopoverCtrl">
  <div class="row">
    <div ng-repeat="currency in popoverCurrencies">
      <div class="clearfix" ng-if="$index % 3 == 0"></div>
      <div class="col-md-4">
        <div class="togglebutton">
          <label>
            <span ng-class="currency.iconClass"></span> <span>{{currency.ccy}}</span>
            <input type="checkbox" checked="currency.build">
          </label>
        </div>
      </div>
    </div>
  </div>
</div>

当您尝试在使用 ng-repeat 的同时观看集合时,它会变得有点复杂。根据 docs:

Each template instance gets its own scope ...

因此,当更新这些其他范围内的值时,手表不会收到通知。一种解决方法是将单独的控制器绑定到 ng-repeat 的每个迭代,如 here. But another, possibly cleaner, approach is to utilize the ng-change directive to intercept the updating of these values. I explained it a bit .

所示