AngularJS Tutorial - Watch value change








The following code shows how to use $watch to watch value change.

Example


<!DOCTYPE html>
<html  ng-app>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.js"></script>
<script type='text/javascript'>
var myApp = angular.module('myApp',[]);
function MyCtrl($scope) {<!--from   w  w  w.ja  va  2  s .c  o  m-->
    $scope.value= 'foo';

    $scope.$watch('value', function(value) {
       console.log(value);
    });
}
</script>
</head>
<body ng-app="myApp">
  <div ng-controller="MyCtrl">
    <input type="radio" ng-model="value" value="foo">
    <input type="radio" ng-model="value" value="bar">
    <input type="radio" ng-model="value" value="baz">
    <hr>
    {{value}}
</div>
</body>
</html>

The code above is rendered as follows: