3

In AngularJS when using ng-app: ng-app found in the document will be used to define the root element to auto-bootstrap as an application.

In some of the application it is being used as data-ng-app.

Is there any difference in both of the following declaration, If Yes what & If No then which one is significant and why ?

1.

 <body ng-app>
     <p>{{ 1 + 2 }}</p>
 </body>

2.

 <body data-ng-app>
     <p>{{ 1 + 2 }}</p>
 </body>
Siguza
  • 21,155
  • 6
  • 52
  • 89
Vaibhav Jain
  • 3,729
  • 3
  • 25
  • 42
  • possible duplicate of [ng-app vs. data-ng-app, what is the difference?](http://stackoverflow.com/questions/16589853/ng-app-vs-data-ng-app-what-is-the-difference) – Yoeri Jan 14 '14 at 12:30
  • 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) – Ani Menon May 29 '16 at 06:11

4 Answers4

4

Both and are the same except for the fact that if you want your template/code to be according to be HTML compliant and follow the best practices. Use data-ng-app.

This is what the official documentation quotes:

"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."

Hope that answers your question.

Cheers!

Amit P
  • 924
  • 2
  • 11
  • 15
3

See the answer:

ng-app vs. data-ng-app, what is the difference?

Only diffrence regarding html5 validation

Community
  • 1
  • 1
Ankit Mittal
  • 662
  • 3
  • 6
1

ng-app isn't strictly valid html. Custom attributes should be prefixed with data- to be valid.

So angular added this syntax so users could still have valid html. (Knockout also uses data- attributes.) There is absolutely no difference in functionality.

Source: w3.org - custom data attributes

Community
  • 1
  • 1
Yoeri
  • 2,249
  • 18
  • 33
0

ng-app and data-ng-app both are similar to each other, the only difference between them is sometimes HTML validators will show an error while using ng-app. so that time you can use data-ng-app.

note: x-ng-app also have the same property. You can use x-ng-app too instead of using data-ng-app.

gopinath
  • 141
  • 1
  • 2
  • 5