The comments display the wrong place in api reference
I saw a code snippet in the $rootScope.
https://docs.angularjs.org/api/ng/type/$rootScope.Scope
in the $watch of methods part. the comments "// This is the listener function" should be on the function(newValue,oldValue). thanks.
// Using a listener function
var food;
scope.foodCounter = 0;
expect(scope.foodCounter).toEqual(0);
scope.$watch(
// This is the listener function ---------------------------------------wrong place ?
function() { return food; },
// This is the change handler----------------------------------------should be here?
function(newValue, oldValue) {
if ( newValue !== oldValue ) {
// Only increment the counter if the value changed
scope.foodCounter = scope.foodCounter + 1;
}
}
);
Hmm, I don't think it is an issue of the wrong place for the comment but rather an issue of wording. I can see how change handler and listener function can be mixed up.
When we look at the $watch signature, its arguments are named as follows:
watchExpression- it can be a functionlistener
so yeh, naming the function that provides value to watch as a listener can be confusing...
As you said, "naming the function that provides value to watch as a listener can be confusing..."
Yes, That is what I means.
As the $watch signature put in the doc,"function() { return food; }" is the watchExpression arguments. But the comment "This is the listener function " really make some angular guys(me and my friends) confused. So I though it would be better understanding to update the comment like "This function provide value to be watch is a watchExpression ". thanks.