Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Understanding Angular $watch

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.19k
    Comment on it

    AngularJS has a concept of powerful data binding. It accomplishes this by keeping a watch on its scope variables. When we change the the variable on $scope object, the $scope object gets automatically changed and vice-versa. Most of the time angular calls the $watch function, but in some cases to  update the new values,  you may need to call it by yourself.

    There are two ways to set up a $watch-

    1.by expression- listens(or watches) the $scope and converts to function.
    2.by function- it simply watches the function and no conversion is done.

    Using $watch:-

    $watch function has three parameters- expression,listener and equality object. The last two parameters are optional.
    1.

    $scope.$watch(test, function(newVal, oldVal) {
        console.log(newVal, oldVal);
    });

    It will watch ‘test’ which is an expression against $scope.

    2.    

    $scope.$watch(function() {
                return $scope.test;
            }, function(newVal, oldVal) {
                console.log(newVal, oldVal);
            });

    Both are same in functionality. All watchers are evaluated when $digest is called.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: