Vaibhav Jain Vaibhav Jain - 2 months ago 31
AngularJS Question

ng-app V/S data-ng-app in AngularJS

In AngularJS when using

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

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


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


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


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.