AngularJS ng-click
Directive
Example
Increase the count variable by one, each time the button is clicked:
<button ng-click="count = count + 1" ng-init="count=0">OK</button>
Try it yourself »
Definition and Usage
The ng-click
directive tells AngularJS what to do when an HTML
element is clicked.
Syntax
<element ng-click="expression"></element>
Supported by all HTML elements.
Parameter Values
Value | Description |
---|---|
expression | An expression to execute when an element is clicked. |
More examples
Example
Execute a function, in AngularJS, when a button is clicked:
<body ng-app="myApp">
<div ng-controller="myCtrl">
<button ng-click="myFunc()">OK</button>
<p>The 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>
Try it yourself »