$watch an object by value using AngularJS
By default, when two complex objects are compared, they are checked for reference equality. If you want to watch an object and instead check for value equality, you must pass true
as a third parameter into the $watch
function, which represents objectEquality
.
$scope.$watch('someObject', function(newVal, oldVal){
console.log('object has changed');
}, true);
By passing in true
for objectEquality
, it sets up the $watch
such that if one of the values within someObject
changes, the function will be triggered.