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 ?
You can use data-ng-, instead of ng-, if you want to make your page HTML valid.
Short Answer:
ng-model
anddata-ng-model
are same and equivalent!Why?
reason for:
data-
prefixHTML5 specification expects any custom attribute to be prefixed by
data-
.reason for: both
ng-model
anddata-ng-model
are same and equivalent.if you want to manipulate html or html-fragments on your server before serving it to the browser, you most definitely want to be using data-ng-xxx attributes instead of just ng-xxx attributes.
From Angularjs Documentation
based on above statement below all are valid directives
1. ng-bind
2. ng:bind
3. ng_bind
4. data-ng-bind
5. x-ng-bind
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 withdata-
, likedata-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.The differences lies in the fact that custom
data-*
attributes are valid in the HTML5 specification. So if you need your markup to be validated, you should use them rather than theng
attributes.