angularjs - What's the difference between these watch expressions? -
scope property:
$scope.$watch('foo', fn)$scope.$watch(function() {return $scope.foo}, fn)
non-scope objects:
$scope.$watch(obj.prop, fn)$scope.$watch(function() {return obj.prop}, fn)
the pair non-scope objects produced different outcomes, in former expression didn't execute when obj.prop changed. why?
$scope.$watch('foo', fn)
this use $parse service watch value of $scope.foo, , compare old values against new.
$scope.$watch(function() {return $scope.foo}, fn)
this same first, uses lambda. function() {return $scope.foo} executed on each $digest, old return values compared return new.
$scope.$watch(obj.prop, fn)
this 1 weird , not-recommended, because behavior depends entirely on type of obj.prop. example if obj.prop === "foo", same $scope.$watch('foo', fn). if obj.prop === function(){ return math.random(); } got weird thing. if expecting angular $watch value of obj.prop changes obj.prop, wont work way.
$scope.$watch(function() {return obj.prop}, fn)
this 1 same $scope.$watch(function() {return $scope.foo}, fn) in angular run lambda function() {return obj.prop} each $digest. old return values compared new. have here proper way watch not on $scope. monitor obj.prop changes (because return value).
Comments
Post a Comment