14

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 !

  • 2
    Possible duplicate of [What is the difference between ng-app and data-ng-app?](http://stackoverflow.com/questions/16184428/what-is-the-difference-between-ng-app-and-data-ng-app) – Phil Mar 16 '16 at 05:38
  • 3
    All angular attribute directives can be writen as `ng-thing` or `data-ng-thing`. Both are 100% equivalent. – GregL Mar 16 '16 at 05:41

4 Answers4

12

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.

Community
  • 1
  • 1
Thieu Nguyen
  • 948
  • 6
  • 13
6

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.

Ahmad Sharif
  • 4,141
  • 5
  • 37
  • 49
2

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.

Shashank
  • 2,010
  • 2
  • 18
  • 38
0

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

Ravi Mane
  • 1,468
  • 2
  • 18
  • 24