我在Angular 5 / Angular Material应用程序中有这个FormArray:
this.form = new FormGroup({
customerNumberContainers: new FormArray([
new FormGroup({
contactTenant: new FormControl('', [Validators.required, Validators.minLength(2)]),
customerNumber: new FormControl('', [Validators.required, Validators.minLength(2)])
}),
]),
...
实际上我不知道如何通过这个FormArray。 我试过这个
<form [formGroup]="form" (ngSubmit)="onSubmit()">
<div formGroupName="customerNumberContainers">
<div *ngFor="let customerNumberContainer of form.controls['customerNumberContainers']; index as i">
<mat-input-container class="full-width-input" style="min-width:100px;">
<input matInput placeholder="Tenant" formControlName="customerNumberContainer[i].contactTenant">
</mat-input-container>
<mat-input-container class="full-width-input" style="min-width:100px;">
<input matInput placeholder="Customernumber" formControlName="customerNumberContainer[i].customerNumber">
</mat-input-container>
</div>
</div>
...
答案 0 :(得分:5)
实际上你应该如何做到这一点:
有一个返回控件数组的方法
get customerNumberContainers(): FormArray {
return this.form.get("customerNumberContainers") as FormArray;
}
您将使用变量 i 来管理数组中的表单组
<form [formGroup]="form" (ngSubmit)="onSubmit()">
<div formArrayName="customerNumberContainers">
<div *ngFor="let customerNumberContainer of customerNumberContainers.controls; index as i" [formGroupName]="i">
<mat-input-container class="full-width-input" style="min-width:100px;">
<input matInput placeholder="Tenant" formControlName="contactTenant">
</mat-input-container>
<mat-input-container class="full-width-input" style="min-width:100px;">
<input matInput placeholder="Customernumber" formControlName="customerNumber">
</mat-input-container>
</div>
</div>
...
您可以创建添加方法和删除方法来管理数组中的表单组
add(data?: any) {
/// data is to set up values to your internal form (useful for edition)
this.customerNumberContainers.push(this.fb.group({
}))
}
remove(index: number) {
this.customerNumberContainers.removeAt(index)
}
我希望这可以帮到你
问候
答案 1 :(得分:1)
更改您的
<div *ngFor="let customerNumberContainer of form.controls['customerNumberContainers']; index as i">
要:
<div *ngFor="let item of getControls(); let i = index">
在.ts文件中添加:
getControls() {
const fa = this.form.get('customerNumberContainers') as FormArray; return
fa.controls;
}
最终结果应为:
<form [formGroup]="form" (ngSubmit)="onSubmit()">
<div formArrayName="customerNumberContainers">
<div *ngFor="let item of getControls(); let i = index">
<div [formGroupName]="i">
<mat-input-container class="full-width-input" style="min-
width:100px;">
<input matInput placeholder="Tenant" formControlName="contactTenant">
</mat-input-container>
<mat-input-container class="full-width-input" style="min-width:100px;">
<input matInput placeholder="Customernumber" formControlName="customerNumber">
</mat-input-container>
</div>
</div>...
希望这会有所帮助:)
答案 2 :(得分:1)
查看工作演示
说明:
formControlName="value"
,则值为字符串。[formControlName]="customerNumberContainer[i].contactTenant"
时,接受变量。答案 3 :(得分:0)
你打算在表单数组中有多个formGroup吗?
this.form = new FormGroup({
customerNumberContainers: new FormArray([
new FormControl('', [Validators.required, Validators.minLength(2)]),
new FormControl('', [Validators.required, Validators.minLength(2)])
])
});
如果不是这样的话。并且记住在更改表单数组的值时使用常量
const arr = <FormArray>this.form;
然后你可以推等......
arr.push(new FormControl(''));
如果你不这样做,某些表单数组函数不会起作用。