javascript - What is the "scope" that gets passed to a $scope.$watch function? -
$scope.$watch( function(scope) {return scope.anumber;}, function(newvalue, oldvalue) {alert("value changed");} ); });
what "scope" $scope.$watch takes in first function? (everything after info tangentially related). know "scope" without $ represent variable, in directive link function (scope, element, attributes, ngcontroller), etc. have no idea "comes from" here. it's connected controller's $scope, how?
also, official doc states "the watchexpression called on every call $digest() , should return value watched. (since $digest() reruns when detects changes watchexpression can execute multiple times per $digest() , should idempotent.)" what's advantage doing return function rather saying $scope.valuetowatch (which doesn't work me, have seen people it).
plunk working watch hell of it, don't need q: http://plnkr.co/edit/y86wr93xliao3wtwvst8?p=preview
for reading same q later: article on $watch: http://tutorials.jenkov.com/angularjs/watch-digest-apply.html
simply same scope $watch
being executed. advantage using 1 instead of $scope
avoids useless closure, same.
i'm not sure understand second question, note these equivalent:
1. $scope.$watch(function(scope) { return scope.prop1.prop2; }, cb); 2. $scope.$watch(function() { return $scope.prop1.prop2; }, cb); 3. $scope.$watch('prop1.prop2', cb);
Comments
Post a Comment