AngularJS Tutorial - Watch checkbox value








The following code shows how to watch checkbox value.

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'>
<!--from  w w w  .j a  va  2  s.  c  o  m-->
function MyCtrl($scope) {
    $scope.$watch(
      function() {
        return $scope.master + $scope.slave; 
      },function() {
        $scope.slave = $scope.master;
    });
}
</script>
</head>
<body>
  <div ng-app="" ng-controller="MyCtrl">
  Check me to check both:
    <input type="checkbox" ng-model="master"><br/>
    <input type="checkbox" ng-model="slave" ng-checked="master">
</div>
</body>
</html>

The code above is rendered as follows: