MOCKSTACKS
EN
Questions And Answers

More Tutorials









Angular ngClick


The ng-click directive attaches a click event to a DOM element.

The ng-click directive allows you to specify custom behavior when an element of DOM is clicked. It is useful when you want to attach click events on buttons and handle them at your controller. This directive accepts an expression with the events object available as $event.

HTML

<input ng-click="onClick($event)">Click me</input>
Controller
.controller("ctrl", function($scope) { 
 $scope.onClick = function(evt) {
 console.debug("Hello click event: %o ",evt);
 }
})


HTML

<button ng-click="count = count + 1" ng-init="count=0">
 Increment
</button>
<span>
 count: {{count}}
</span>


HTML

<button ng-click="count()" ng-init="count=0">
 Increment
</button>
<span>
 count: {{count}}
</span>


Controller

...
$scope.count = function(){
 $scope.count = $scope.count + 1;
}
...


When the button is clicked, an invocation of the onClick function will print "Hello click event" followed by the event object.


Conclusion

In this page (written and validated by ) you learned about AngularJS ngClick . What's Next? If you are interested in completing AngularJS tutorial, your next topic will be learning about: AngularJS ngList.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.