angular.js icon indicating copy to clipboard operation
angular.js copied to clipboard

The comments display the wrong place in api reference

Open malaikuangren opened this issue 12 years ago • 2 comments

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;
               }
             }
           );

malaikuangren avatar Jul 18 '14 08:07 malaikuangren

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 function
  • listener

so yeh, naming the function that provides value to watch as a listener can be confusing...

pkozlowski-opensource avatar Jul 20 '14 12:07 pkozlowski-opensource

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.

malaikuangren avatar Jul 21 '14 01:07 malaikuangren