Reputation: 31
I want to add a form inside a *ngFor loop. Below is my attempt to repeat a button, like so:
<div *ngFor="let passenger of passengerForm;let i=index;">
<form>
<mat-form-field>
<input matInput type="text" placeholder="Enter Name"
[(ngModel)]="passenger.Name">
</mat-form-field>
</Form>
<div>
<button (click)="addFieldValue(i); ">
Add Passenger
</button>
</div>
</div>
newAttribute ={}
addFieldValue(index) {
if ( index < 3) {
this.passengerForm.push(this.newAttribute )
this.newAttribute = {};
}
}
Upvotes: 1
Views: 247
Reputation: 320
<div *ngFor="let passenger of passengerForm;let i=index;">
<input matInput type="text" placeholder="Enter Name" [(ngModel)]="passenger.Name">
</div>
Try this way. You need to wrap the input field only.
Upvotes: 1