我想用angular $ watch观看kendo多选值变化,但它不起作用。 我的代码中有什么问题。我该如何解决?
代码如下(或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>
答案 0 :(得分:2)
在true
侦听器功能之后插入$watch
:
$scope.$watch('ValueA', function() {
console.log('ValueA Changed');
$scope.valueAChangeCnt++;
}, true);
这将比较对象的相等性而不是参考。 From AngularJS API