Reputation: 581
This is my Angular2 application with input fields inside table. My data is displaying on select
tag but the data binded using ngModel
on input
tag is not been displayed in input
field.
<form name="userForm">
<table>
<tr *ngFor="let item of itemList; let in =index">
<td><select><option >{{item.FirstName}}</option></select></td>
<td><input type="text" id="lastname" name="lastname" [(ngModel)]="itemList[in].lastname"></td>
<td><input type="text" id="middlename" name="middlename" [(ngModel)]="itemList[in].middlename"></td>
</tr>
</table>
</form>
Upvotes: 34
Views: 33416
Reputation: 1584
An alternative for Yaroslav's method: set [ngModelOptions]="{standalone: true}"
to input
P.S. make sure you know what standalone option is https://angular.io/api/forms/NgModel#options
Upvotes: 0
Reputation: 21476
When creating multiple ngModel controls inside ngFor loop make sure to give each control unique name
:
<form name="userForm">
<table>
<tr *ngFor="let item of itemList; let in = index">
<td><input type="text" name="lastname-{{in}}" [(ngModel)]="item.lastname"></td>
<td><input type="text" name="middlename-{{in}}" [(ngModel)]="item.middlename"></td>
</tr>
</table>
</form>
Upvotes: 86