我有多行的 Mat 表,带有添加按钮,单击它,添加一个新行。我想为所有行添加验证,现在我下面的代码只对任何一行进行验证。
我的 component.html
<form [formGroup]="auditUserValidation">
<mat-table [dataSource]="dataSource">
<ng-container matColumnDef="Audit">
<mat-header-cell *matHeaderCellDef> Name </mat-header-cell>
<mat-cell *matCellDef="let element">
<mat-select formControlName="name" placeholder="Pls select">
<mat-option [value]="audit" *ngFor="let name of nameList">{{name.firstname}}</mat-option>
</mat-select></mat-cell>
</ng-container>
<ng-container matColumnDef="Country">
<mat-header-cell *matHeaderCellDef> Country</mat-header-cell>
<mat-cell *matCellDef="let element">
<mat-select formControlName="Country" placeholder="Pls select">
<mat-option [value]="audit" *ngFor="let country of CountryList">{{country.name}}</mat-option>
</mat-select></mat-cell>
</ng-container>
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
</mat-table>
Save button which i want to disable if all rows not selected
<button type="button" (click)="addElement()"> <i class="material-icons">add</i>Add</button>
<button type="button" [disabled]='auditUserValidation.invalid' (click)="submitReport()">Save</button>
我的 component.ts 文件
auditUserValidation: FormGroup;
constructor(private formBuilder: FormBuilder }
ngOnInit() {
this.auditUserValidation = this.formBuilder.group({
name: ['', [Validators.required]],
Country: ['', [Validators.required]],
});
}