Can I use ng-click and onclick together
Asked Answered
C

2

11

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 ?

Councilman answered 29/10, 2014 at 21:39 Comment(1)
Out of curiosity, why do you need to do this?Wilsonwilt
H
12

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>
Homerus answered 29/10, 2014 at 22:1 Comment(0)
C
0

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.

Calabro answered 2/6, 2017 at 9:35 Comment(1)
you can have a try, but it's not necessary that you take them togetherCalabro

© 2022 - 2024 — McMap. All rights reserved.