Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

html

angularjs

I'm currently looking at this start tutorial video for angular.js

At some moment (after 12'40"), the speaker states that the attributes ng-app and data-ng-app="" are more or less equivalent inside the <html> tag, and so are ng-model="my_data_binding and data-ng-model="my_data_binding". However The speaker says the html would be validated through different validators, depending on which attribute is used.

Could you explain the difference between the two ways, ng- prefix against data-ng- prefix ?

like image 430
Stephane Rolland Avatar asked Oct 14 '22 19:10

Stephane Rolland


People also ask

What is the difference between ng-model and data NG model?

For AngularJS there is no difference between ng-app and data-ng-app or ng-controller and data-ng-controller or ng-model and data-ng-model because while compiling HTML page, AngularJS strips data- or x- prefix. Find the URL for reference.

What is data ng-app?

Definition and Usage. The ng-app directive tells AngularJS that this is the root element of the AngularJS application. All AngularJS applications must have a root element. You can only have one ng-app directive in your HTML document. If more than one ng-app directive appears, the first appearance will be used.

What is Ng-app and NG-model in AngularJS?

AngularJS DirectivesThe ng-app directive initializes an AngularJS application. The ng-init directive initializes application data. The ng-model directive binds the value of HTML controls (input, select, textarea) to application data. Read about all AngularJS directives in our AngularJS directive reference.

Can ng-app be nested?

The first ngApp found in the document will be used to define the root element to auto-bootstrap as an application. To run multiple applications in an HTML document you must manually bootstrap them using angular. bootstrap instead. AngularJS applications cannot be nested within each other.


1 Answers

Good question. The difference is simple - there is absolutely no difference between the two except that certain HTML5 validators will throw an error on a property like ng-app, but they don't throw an error for anything prefixed with data-, like data-ng-app.

So to answer your question, use data-ng-app if you would like validating your HTML to be a bit easier.

Fun fact: You can also use x-ng-app to the same effect.

like image 407
Code Whisperer Avatar answered Oct 16 '22 07:10

Code Whisperer