AngularJS ng-click Directive
Definition and Usage
ng-click
The directive tells AngularJS what operation to perform when clicking an HTML element.
Instance
Example 1
The variable count is incremented by 1 each time the button is clicked:
<button ng-click="count = count + 1" ng-init="count=0">OK</button>
Example 2
In AngularJS, a function is executed when a button is clicked:
<body ng-app="myApp"> <div ng-controller="myCtrl"> <button ng-click="myFunc()">OK</button> <p>This button has been clicked {{count}} times.</p> </div> <script> angular.module('myApp', []) .controller('myCtrl', ['$scope', function($scope) { $scope.count = 0; $scope.myFunc = function() { $scope.count++; }); }); </script> </body>
Syntax
<element ng-click="expression</element>
All HTML elements support it.
Parameters
Parameters | Description |
---|---|
expression | Expression to be executed when an element is clicked. |