Difference between ng-model and data-ng-model

后端 未结 4 1067
谎友^
谎友^ 2020-12-29 19:50

I am new with the AngularJs. Can anyone say the difference between ng-model and data-ng-model?

With ng-model

First          


        
相关标签:
4条回答
  • 2020-12-29 20:27

    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:

    1. Strip x- and data- from the front of the element/attributes.
    2. Convert the :, -, or _-delimited name to camelCase.
    0 讨论(0)
  • 2020-12-29 20:38

    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">
    
    0 讨论(0)
  • 2020-12-29 20:39

    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.

    0 讨论(0)
  • 2020-12-29 20:45

    There is no difference between ng-model and data-ng-model if you see in terms of AngularJs.

    Actually, 'data' used as prefix to validate HTML5 validation. So, it is good practice to use data-ng-model, however, you can use ng-model as well. There is no problem in that also.

    Both have the same meaning and both have the same output:

    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">  
    
    0 讨论(0)
提交回复
热议问题