0

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?

1 Answer 1

5

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);
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.