我想在* ngFor循环内添加一个表单。以下是我重复按钮的尝试,例如:
<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 = {};
}
}
答案 0 :(得分:1)
<div *ngFor="let passenger of passengerForm;let i=index;">
<input matInput type="text" placeholder="Enter Name" [(ngModel)]="passenger.Name">
</div>
尝试这种方式。您只需要包装输入字段即可。