AngularJS Tutorial - Respond to Click Events using Controllers








The following code shows how to respond to Click Events using Controllers.

Example


<!--from w  w  w  .  j  a va  2s .  com-->
To hide an HTML  element on button click.
<!doctype html>
<html ng-app>
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.js"></script>
    <script>
function MyCtrl($scope) {
  $scope.visible = true;

  $scope.toggle = function() {
    $scope.visible = !$scope.visible;
  };
}    
    </script>
  </head>
  <body ng-app>
    <div ng-controller="MyCtrl">
      <button class="btn" ng-click="toggle()">Toggle</button>
      <p ng-show="visible">Hello World!</p>
    </div>
  </body>
</html>

The code above is rendered as follows: