user5733024
user5733024

Reputation:

Angular Js difference between ng-init and data-ng-init

I am leaning Angular Js. I found many authors use ng-init and many authors use data-ng-init. I tried to search difference but could not get appropriate answer. If someone knows difference between them, then please share your answer.

Thanks !

Upvotes: 14

Views: 12878

Answers (4)

Ravi Mane
Ravi Mane

Reputation: 1536

HTML5 validators will throw an error on ng-app. when we use data- as prefix it simply ignore the throw an error

Upvotes: 0

Shashank
Shashank

Reputation: 2060

Ideally there is no difference between the two in terms of functionality but with only validation.

After the inception of HTML5, the code editor like Visual Studio, highlights the 'ng-', as something which is invalid. But actually it's valid, so there is a way to make the code editor understand that the attribute of AngularJS is a valid one by prefixing it with 'data-ng-*'.

So when used the prefix, in any HTML5 code editor, it doesn't underlines the attributes and treats them as valid.

This was the original purpose of the 'data-*' prefix.

Upvotes: 2

Thieu Nguyen
Thieu Nguyen

Reputation: 1078

Interesting question. From W3schools:

You can use data-ng-, instead of ng-, if you want to make your page HTML valid.

HTML Validators will throw an error with property like ng-init, but if we give a prefix with data-(e.g: data-ng-init), HTML Validators accept it and it will be valid.

You can read more details at here: https://www.w3.org/TR/2011/WD-html5-20110525/elements.html#embedding-custom-non-visible-data-with-the-data-attributes

Note: data-* attributes are the way to extend HTML.

Upvotes: 12

Ahmad Sharif
Ahmad Sharif

Reputation: 4435

data-ng-init is HTML5 validate code where ng-init does not valid code in terms of HTML5. This is applicable for all angular directive.

Upvotes: 6

Related Questions