Respond to Click Events using Controllers

Description

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

Example


<!-- w w  w. j a va 2s  .c  o m-->
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>

Click to view the demo





















Home »
  AngularJS »
    AngularJS Example »




Controller
Directives
Expression
Filter
Form
Inject
Scope
Server
Style
Template
Utilities