javascript - variable change not notified to `$scope.$watch` -
i handling jqrangeslider through angular directive + controller.
within controller, assigning new values scope variables there change not notified controller scope (i using isolated scope '='
in directive) though correctly updated.
// jqrangeslider event handler var valueschanged = function(e, data) { if (data.values.min && data.values.max) { console.log('values changed', data.values); // printed $scope.minselectedtime = data.values.min; // not triggering $scope.$watch $scope.maxselectedtime = data.values.max; $scope.$broadcast('minchanged', $scope.minselectedtime); // instantly triggering $scope.$on('minchanged',...) console.log('scope', $scope); } }; $scope.$watch('minselectedtime', function(newvalue, oldvalue) { if (newvalue === oldvalue) { return; } console.log('minselectedtime -->', newvalue); }, true); $scope.$on('minchanged', function(event, min) { console.log('minchanged ==>', min); });
$scope.$watch('minselectedtime', ...)
triggered on next $scope
modification (where $scope.minselectedtime
not modified):
$scope.toggletimescale = function(refinementstring) { console.log('scale set to', refinementstring); $scope.timescale = refinementstring; // year, month, day... }
why isn't $scope.$watch
immediatly notified of variable change?
isolated code has $scope.minselectedtime
value changed parent scope doesn't see value changed until $scope.toggletimescale
triggered.
your method of using $scope.$apply() correct. reason being update jqrangeslider happened 'outside' of angularjs knowledge. if read documentation on $apply method (http://docs.angularjs.org/api/ng.$rootscope.scope#$apply) you'll see it's used in case need.
Comments
Post a Comment