LearnAngular icon indicating copy to clipboard operation
LearnAngular copied to clipboard

Ambiguous syntax explanation in the $watch tutorial

Open ColinDTaylor opened this issue 9 years ago • 3 comments

In the tutorial for $watch, a functioning example is never given. This is fine, but later on when explaining the syntax of a $watch expression, there's this example:

$scope.$watch([expression returning watched value],
              [change handler],
              [objectEquality?]);

This teaches the syntax ambiguously, as it's hard to tell whether to input something like this (correct):

angular.module("root", [])
    .controller("index", ["$scope", function($scope) {
        $scope.$watch("factor", function (newValue) {
            $scope.product = newValue * 2;
        });

        $scope.factor = 6;
    }]);

or this (incorrect):

angular.module("root", [])
    .controller("index", ["$scope", function($scope) {
        $scope.$watch(["factor"], 
                      [function (newValue) {
                           $scope.product = newValue * 2;
                      }]);

        $scope.factor = 6;
    }]);

ColinDTaylor avatar Mar 17 '16 01:03 ColinDTaylor

Tried several times to put an array for the "factor" too, and i get the error, until i searched the Api for the function: https://docs.angularjs.org/api/ng/type/$rootScope.Scope

"$watch(watchExpression, listener, [objectEquality]);"

GauntStrelok avatar Jun 02 '16 14:06 GauntStrelok

Good point. If either of you want to change it just do a PR and I'll accept and deploy. Otherwise I'll take care of it when I get the time.

levibotelho avatar Jun 16 '16 19:06 levibotelho

i later figured out how to use $watch thank you i understood more going into the documentation.

prayagKhanal avatar Jun 25 '16 02:06 prayagKhanal