1

我想用角度 $watch 观察剑道多选值的变化,但它不起作用。我的代码有问题吗。我该如何解决?

代码如下(或jsbin

值 A 在第一次调用后更改秒表。

    <body ng-app="myapp">
      <div ng-controller="mycontroller">
    ValueA :{{ ValueA }} , Change count: {{ valueAChangeCnt }}<br />

          <div  multiple="multiple" 
                kendo-multi-select
                data-placeholder="Select attendees..."
                k-data-text-field = "'name'",
                k-data-value-field = "'index'",
                k-data-source='dsA'
                ng-model='ValueA'
          >
          </div>
    <br />
    <br />
    ValueB :{{ ValueB }} , Change count: {{ valueBChangeCnt }}<br />
    <br />
        <select ng-model="ValueB">
            <option value="1">A</option>
            <option value="2">B</option>
            <option value="3">C</option>
            <option value="4">D</option>
            <option value="5">E</option>
            <option value="6">F</option>
        </select>
      </div>
    </body>

<script type="text/javascript">
    var MyApp = angular.module('myapp', ['kendo.directives']);

    function mycontroller($scope) {

        $scope.dsA = new kendo.data.DataSource({
            data: [
                { name: "A", index: 1 },
                { name: "B", index: 2 },
                { name: "C", index: 3 },
                { name: "D", index: 4 },
                { name: "E", index: 5 },
                { name: "F", index: 6 }
            ]
        });
        $scope.valueAChangeCnt = 0;
        $scope.$watch('ValueA', function() {
            console.log('ValueA Changed');
            $scope.valueAChangeCnt++;
        });

        $scope.valueBChangeCnt = 0;
        $scope.$watch('ValueB', function() {
            console.log('ValueB Changed');
            $scope.valueBChangeCnt++;
        });

    }

    mycontroller.$inject = ['$scope'];
</script>
4

1 回答 1

2

在监听函数true之后插入:$watch

    $scope.$watch('ValueA', function() {
        console.log('ValueA Changed');
        $scope.valueAChangeCnt++;
    }, true);

这将比较对象是否相等而不是参考。来自 AngularJS API

于 2013-10-29T01:53:19.750 回答