【发布时间】:2019-07-23 18:03:21
【问题描述】:
我有一个带有 FormArray 的 FormGroup 和一个显示数组的 mat-table。 当我将新的 FormGroup 添加到 FormArray 时,mat-table 不会添加新行。
我试图为 trackBy 做广告,但我不知道在哪里(老实说也不知道为什么)。
component.html:
<form [formGroup]="formGroup">
<div formArrayName="items">
<button mat-raised-button (click)="addNew()">New Case</button>
<table mat-table [dataSource]="items.controls">
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns"></mat-row>
<ng-container matColumnDef="itemKey">
<mat-header-cell *matHeaderCellDef> Key </mat-header-cell>
<mat-cell *matCellDef="let item; let i = index" [formGroupName]="i">
<mat-form-field>
<input formControlName="itemKey" matInput />
</mat-form-field>
</mat-cell>
</ng-container>
和component.ts:
formGroup = new FormGroup({
items: new FormArray()
});
get items() { return this.formGroup.get("items") as FormArray }
addNew() {
this.items.push(new FormGroup({
itemKey: new FormControl(),
itemName: new FormControl()
}));
}
【问题讨论】:
标签: angular typescript angular-material angular7 angular-forms