Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use ng-click and onclick together

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 ?

like image 871
inoxious Avatar asked Oct 29 '14 21:10

inoxious


People also ask

Can Ng-click have two functions?

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.

Can we use Onclick in Angular?

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.

Can we put condition in NG-click?

We can add ng-click event conditionally without using disabled class.

What is the use of NG-click?

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.


1 Answers

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>
like image 75
Kursad Gulseven Avatar answered Oct 05 '22 23:10

Kursad Gulseven