Using $watchCollection to Track Changes to Properties of an Object in the Scope
You can also watch the properties of an object using the $watchCollection
method. The $watchCollection
method takes an object as the first parameter and watches the properties of the object. In the case of an array, the individual values of the array are watched. For example:
$scope.scores = [5, 10, 15, 20];$scope.$watchGroup('scores', function(newValue, oldValue) { $scope.newScores = newValue;});
Get Learning AngularJS now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.