Reputation: 9007
I'm setting watch for $scope
object will not trigger $watch
change event unless the whole value is changed
Example :
//Some where in .run function i set rootScope and set $watch too.
$rootScope.config = {date:'11/4/2015',moreValues:'etc'};
//setting $watch
$rootScope.$watch('config',function(new,old) {
console.log('config value changed :)',new);
});
//----->Inside Controller----------
//NOw THIS WILL NOT TRIGGER $watch
$rootScope.config.date = 'another day';
//Same as this, it will also not trigger $watch
var temp = $rootScope.config;
temp.date = 'another day';
$rootScope.config = temp;
//YET THIS WILL WORK JUST FINE :) AND WILL TRIGGER $watch
var temp = angular.copy($rootScope.config);
temp.date = 'another day';
$rootScope.config = temp;
can someone tell me why is this behavior? is there a better way to trigger $watch
on change of object Property?
Upvotes: 0
Views: 1229
Reputation: 388336
You can use $watchCollection, or pass a third param as true
$rootScope.$watch('config',function(value,old) {
console.log('config value changed :)',value);
}, true);
or
$rootScope.$watchCollection('config',function(value,old) {
console.log('config value changed :)',value);
});
Upvotes: 5