I am working on one small angularjs app. I have a button, where I am using 2 events, ng-click, and onlick. It works right and there are no issues, but I want to be sure 100% I am doing it good and is my approach right ? Is it allowed to have them together at a button state events ?
In this article, we will learn how to get many/multiple functions to the ng-click directive passed, in just one click. The key is to add a semi-colon (;) or a comma (,). All the functions must be separated by a (;) or a (, ). This syntax is supported by all the elements in the HTML.
Introduction to AngularJs onclickThe ng-click directive can be used with multiple HTML elements such as button, input, select, checkbox, etc. This provides the developer ability to define custom behavior whenever an element is clicked.
We can add ng-click event conditionally without using disabled class.
The ng-click Directive in AngluarJS is used to apply custom behavior when an element is clicked. It can be used to show/hide some element or it can pop up an alert when the button is clicked.
You can bind as many events as you wish to a button etc. But this is superfluous. See this jsFiddle: Bind events
JS:
var myApp = angular.module('myApp',[]);
function MyCtrl($scope) {
$scope.ngFn = function () {
console.log("ngFn is triggered!");
};
}
function nativeFn() {
console.log("nativeFn is triggered!");
};
$(document).ready(function() {
$('#forJq').bind('click', function () {
console.log("Anonymous function bind by jq is triggered!");
});
});
HTML:
<div ng-controller="MyCtrl">
<button id="forJq" onclick="nativeFn()" ng-click="ngFn()">Try me!</button>
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With