Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
38
rated 0 times [  44] [ 6]  / answers: 1 / hits: 169636  / 11 Years ago, fri, october 18, 2013, 12:00:00

I want to watch for changes in a dictionary, but for some reason watch callback is not called.



Here is a controller that I use:



function MyController($scope) {
$scope.form = {
name: 'my name',
surname: 'surname'
}

$scope.$watch('form', function(newVal, oldVal){
console.log('changed');
});
}


Here is fiddle.



I expect $watch callback to be fired each time name or surname is changed, but it doesn't happen.



What is the correct way to do it?


More From » angularjs

 Answers
16

Call $watch with true as the third argument:



$scope.$watch('form', function(newVal, oldVal){
console.log('changed');
}, true);


By default when comparing two complex objects in JavaScript, they will be checked for reference equality, which asks if the two objects refer to the same thing, rather than value equality, which checks if the values of all the properties of those objects are equal.



Per the Angular documentation, the third parameter is for objectEquality:




When objectEquality == true, inequality of the watchExpression is determined according to the angular.equals function. To save the value of the object for later comparison, the angular.copy function is used. This therefore means that watching complex objects will have adverse memory and performance implications.



[#74891] Thursday, October 17, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
estefanib

Total Points: 508
Total Questions: 104
Total Answers: 83

Location: Lebanon
Member since Sun, Aug 2, 2020
4 Years ago
;