AngularJS Tutorial - Change watched value








The following code shows how to Change watched value.

Example


<!DOCTYPE html>
<html  ng-app>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.js"></script>
</head><!--from   ww w  . j a  v  a2s. com-->
<body ng-app="myApp">
   <div ng:controller="Main">
    <p>Your lucky number: <input ng-model="luckynumber"> (Your number is {{luckynumber}})</p>
</div>
<script type='text/javascript'>
angular.module('myApp', []);

function Main($scope) {
    $scope.luckynumber = null;
    $scope.$watch('luckynumber', function() {
        if ($scope.luckynumber == 7) {
            console.log('The lucky number mustn\'t be equal 7.');
            $scope.luckynumber = null;
        }
    })
}
</script>
</body>
</html>

The code above is rendered as follows: