I have an array with boolean values and what I need to do is try to use the $watch function in order to show a message when some change happens in the array.
As you can see in this code the array changes its values but the $watch function doesn't work. Any idea?
View
<div data-ng-repeat="catA in categoryA">
<b><input type="checkbox" checked="checked" ng-model="arrayCategoryA[$index]"/>{{catA.name}}</b>
</div>
Controller
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
takes a watch expression and in this case is the name of the scope variable of arrayCategoryA
.
$scope.$watchCollection(`arrayCategoryA`, function(newVal, oldVal){
console.log("something changed");
}, true);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.