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 ?
Can I use ng-click and onclick together
Asked Answered
Out of curiosity, why do you need to do this? –
Wilsonwilt
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>
For a single btn, it's ok to use ng-click
or onclick
in the ng-app
. There is no difference between the two functions. For effective team work, you,d better to have an account with each other. In Angular apps, ng-click
is recommended.
you can have a try, but it's not necessary that you take them together –
Calabro
© 2022 - 2024 — McMap. All rights reserved.