[英]AngularJS: Array and deep $watch doesn't work
我有一個帶有布爾值的數組,我需要做的是嘗試使用$ watch函數,以便在數組中發生某些更改時顯示一條消息。
如您在此代碼中所見,數組更改了其值,但$ watch函數不起作用。 任何想法?
視圖
<div data-ng-repeat="catA in categoryA">
<b><input type="checkbox" checked="checked" ng-model="arrayCategoryA[$index]"/>{{catA.name}}</b>
</div>
控制者
app.controller("controllerApp", function($scope, $http, $filter){
$scope.arrayCategoryA = [];
$scope.$watchCollection($scope.arrayCategoryA, function(newVal, oldVal){
console.log("something changed");
}, true);
$http.get("categoryA.json").success(function(data) {
$scope.categoryA = data;
for (var i = 0; i < $scope.categoryA.length; i++)
$scope.arrayCategoryA[i] = true;
});
});
$watchCollection
一個監視表達式,在這種情況下,它是arrayCategoryA
的作用域變量的arrayCategoryA
。
$scope.$watchCollection(`arrayCategoryA`, function(newVal, oldVal){
console.log("something changed");
}, true);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.