javascript - What happens when $scope.$watch expression depends on multiple variables? -
if in controller, have like:
$scope.service = service; $scope.$watch('service.properties[service.selectedproperty]', function(newval,oldval){}); what happens here? both service.properties , service.selectedproperty can change. watch end watching both service.properties , service.selectedproperty?
i have tested , watch seems execute when either variable changes, want. wondering if there unintended consequences when using .$watch way, , how works.
does watch end watching both
service.properties,service.selectedproperty?
in fact, watching neither, result of evaluating expression whole. explain:
angular watches use dirty checking principle: if expression whole evaluated else when last evaluated, change has happened, , associated handlers run. (this behaviour more apparent when use form of $watch takes function first argument - returning value watched.)
that means technically there exist situation service.properties[service.selectedproperty] evaluated same before, service.properties, associative object, and service.selectedproperty, property key have both (or either of them) changed in meantime - yet, expression evaluated same. in such cases, change event not triggered.
for example, given:
var myobj = { a: 123, b: 123 }; var key = 'a'; the value of expression myobj[key] stay same if change key 'b' instead of 'a'.
as long expression evaluates same, viewpoint of angular, nothing has changed. , intents , purposes, nor should matter - if does, watch expression set improperly, , want watch more generic expression.
Comments
Post a Comment