*ngFor how to bind each item in array to ngModel using index

后端 未结 2 614
眼角桃花
眼角桃花 2020-12-29 04:11

===final updated==

http://plnkr.co/edit/WKRBB7?p=preview

since I use ngModel in a form, I must add name attribue.

and my mistake is that

相关标签:
2条回答
  • 2020-12-29 04:29

    in you case sir if you are using *ngFor for loop then i don't think so you need index. why don't you just use x.Name. here is the modified code.

    <table>
           <tr *ngFor="let x of names;let i = index;">
             <td>{{ i+ 1 }}</td>
             <td><input [(ngModel)]="x.Name">{{x.Name}}</td>
           </tr>
        </table>
    

    or can you try this

    <table>
               <tr *ngFor="let x of names;let i = index;">
                 <td>{{ i+ 1 }}</td>
                 <td><input [value]="x.Name" [(ngModel)]="x.Name">{{x.Name}}</td>
               </tr>
            </table>
    
    0 讨论(0)
  • 2020-12-29 04:32

    It should be [ngModel]="..."

    <table>
       <tr *ngFor="let x of names;let i = index;">
         <td>{{ i+ 1 }}</td>
         <td><input [(ngModel)]="names[i].Name">{{x.Name}}</td>
       </tr>
    </table>
    
    0 讨论(0)
提交回复
热议问题