I have an array of ten objects. Each one of them contains six properties and display everything on view. User is able to change the properties by typing new data on inputs. How can I watch whole array and identify which property on which object has been changed and don't repeat my code 10 times in order to watch each object separately?
Asked
Active
Viewed 49 times
2 Answers
5
You can use $watchCollection
$scope.$watchCollection('data', function (newVal, oldVal) { /*...*/ });

Sajeetharan
- 216,225
- 63
- 350
- 396
2
Usually it's a bad idea to watch big collections of objects since angularjs will perform equality checks on every digest loop. If you really need to do that, the $scope.$watch
function has the third parameter, objectEquality
, where you can pass the boolean function which returns true if the old value is equal to the new value. You can also just pass true
to the third parameter.
$scope.$watchCollection(obj, listener);
is another alternative.

ganqqwerty
- 1,894
- 2
- 23
- 36