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?
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.
The ng-click directive tells AngularJS what to do when an HTML element is clicked.
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.
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.
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.
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.
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