I am new with the AngularJs. Can anyone say the difference between ng-model and data-ng-model?
With ng-model
First Name : <input type="text" ng-model="fname" id="fname">
Second Name : <input type="text" ng-model="lname" id="lname">
With data-ng-model
First Name : <input type="text" data-ng-model="fname" id="fname">
Second Name : <input type="text" data-ng-model="lname" id="lname">
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 .
ngModel is a directive which binds input, select and textarea, and stores the required user value in a variable and we can use that variable whenever we require that value. It also is used during validations in a form. We can use ngModel with: input. text.
Please note that 'ng-value' does have an advantage over hard-coding to the element attribute 'value', in that you can specify non-string types. For example, 'true' and 'false' will be stored to the model as booleans, rather than as strings. Also ngValue is a one-way binding, and ngModel is a two-way binding.
Angular NgModel is an inbuilt directive that creates a FormControl instance from the domain model and binds it to a form control element. The ngmodel directive binds the value of HTML controls (input, select, textarea) to application data. We can merely achieve it in the component element and the HTML element both.
while both ng-model
and data-ng-model
would work, HTML5 expects any custom attribute to be prefixed by data-
.
<!-- not HTML5 valid --> <input type="text" ng-model="name"> <!-- HTML5 valid --> <input type="text" data-ng-model="name">
They are the same. Angular strips the data-
part from the attribute. From the docs:
Angular normalizes an element's tag and attribute name to determine which elements match which directives... The normalization process is as follows:
- Strip
x-
anddata-
from the front of the element/attributes.- Convert the
:
,-
, or_
-delimited name tocamelCase
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With