To manage the input
event on the final line, you can do the following:
<div *ngFor="let header of headers; let i = index; let isLast = last" fxLayout="row" fxLayoutGap="25px">
<mat-form-field fxFlex="25">
<input matInput placeholder="Name" type="text" name="name-{{i}}" value="{{header.name}}"
(input)="handleInput(isLast)">
</mat-form-field>
<mat-form-field fxFlex>
<input matInput placeholder="Value" type="text" name="value-{{i}}" value="{{header.value}}">
</mat-form-field>
...
</div>
utilize the handleInput
function, which inserts a new entry upon the first modification of the input content:
export class DynamicformComponent implements OnInit {
public headers: any[] = [];
ngOnInit() {
this.headers = [
{ name: 'Accept-Encoding', value: 'gzip' },
{ name: 'Accept-Charset', value: 'utf-8' },
{ name: 'User-Agent', value: '' },
{ name: 'Accept', value: 'text/plain' },
{ name: 'Cookie', value: '' },
{ name: '', value: '' }
];
}
handleInput(isLast: boolean) {
if (isLast) {
this.headers.push({ name: '', value: '' });
}
}
eraseInput(index) {
this.headers.splice(index, 1)
}
}
View the demonstration on this stackblitz.