angularjs ng-repeat use $index inside ng-model directive

后端 未结 3 1692
走了就别回头了
走了就别回头了 2021-01-27 00:14

Try to use ng-repeat

相关标签:
3条回答
  • 2021-01-27 00:27

    Try using ng-init to reference $index

    <li ng-repeat = "n in [1,2,3,4] ng-init="myIndex = $index">
      <label ng-model="{{'p'+ myIndex }}" />
    </li>
    
    0 讨论(0)
  • 2021-01-27 00:40

    This is not working because you have used ng-model on label tag which is not possible.

    ng-model can be only used on input, select and textarea as per This

    you cannot use it on div's, ul, ol, tabel etc.

    Instead, to assign the value in label you can use ng-value like this:

    <label ng-value="{{'p'+ n }}"> {{n}} </label>
    

    or to assign index value to ng-model you can do:

    <input type="text" ng-model="user.text[$index]"/>
    

    to display the index you can do:

    {{$index}}
    
    0 讨论(0)
  • 2021-01-27 00:43

    ng-model is an Angular attribute, you don't need to include the {{}} -- try ng-model="'p' + n"

    0 讨论(0)
提交回复
热议问题