javascript - angularjs $watch cannot detect change of Property in an object -
i'm setting watch $scope
object not trigger $watch
change event unless whole value changed
example :
//some in .run function set rootscope , 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 not trigger $watch $rootscope.config.date = 'another day'; //same this, not trigger $watch var temp = $rootscope.config; temp.date = 'another day'; $rootscope.config = temp; //yet work fine :) , trigger $watch var temp = angular.copy($rootscope.config); temp.date = 'another day'; $rootscope.config = temp;
can tell me why behavior? there better way trigger $watch
on change of object property?
you can use $watchcollection, or pass third param 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); });
Comments
Post a Comment