Vaibhav Jain Vaibhav Jain - 16 days ago 5
AngularJS Question

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

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>

Answer

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!

Comments