Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS, ng-click vs ng:click vs ngClick

Tags:

angularjs

I'm learning AngularJS, and I'm a little confused by the different usages of directives that I'm coming across.

For example, sometimes I see something like (ng colon click):

<tr ng:click="..." ...>

sometimes I see (ng dash click):

<tr ng-click="..." ...>

and in the Angular docs, directives are shown as "ngClick" (camelcase with no dash or colon). Additionally, in some places, I've seen: data-ng-click

What's the difference between these different forms?

like image 489
blindsnowmobile Avatar asked May 16 '14 20:05

blindsnowmobile


People also ask

What is the difference between Ng-click and Onclick?

Another significant difference between ng-click and onclick is the execution context. Code inside an onclick attribute executes against the global window object, while an expression inside of ng-click executes against a specific scope object, typically the scope object representing the model for the current controller.

What is Ng-click in AngularJS?

The ng-click directive tells AngularJS what to do when an HTML element is clicked.

Can we use NG-click and Onclick together?

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.

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.


2 Answers

No difference, it all depends on your style of programming. ng-click, I think, is by far the most popular style.

When you're creating your own directive, you should always camelcase it in the javascript, and then when you put it on an element in your html you should use the lowercase version separated by your favorite flavor. I always do it like so:

angular.module('Test', []).directive('testDirective', function(){

});

and then:

<div test-directive></div>

From the angular docs:

Best Practice: Prefer using the dash-delimited format (e.g. ng-bind for ngBind). If you want to use an HTML validating tool, you can instead use the data-prefixed version (e.g. data-ng-bind for ngBind). The other forms shown above are accepted for legacy reasons but we advise you to avoid them.

like image 184
Joe Avatar answered Oct 21 '22 02:10

Joe


ng-click, ng:click and ngClick are all the same for AngularJS, you can use whichever one you prefer, though I think ng-click is how you'll usually see it being used.
data- is an HTML5 prefix you can use to embed custom data, it helps making sure your HTML passes validation and keeping some IDEs from showing errors for unknown attributes.

like image 34
Vitor M. Barbosa Avatar answered Oct 21 '22 02:10

Vitor M. Barbosa