【发布时间】:2023-02-01 00:23:30
【问题描述】:
我正在使用 mat-chip-grid 来编辑 FormArray<FormControl<string>> 的值,这很好,但我不知道如何显示错误。我正在使用文档中的修改示例:
<mat-form-field>
<mat-label>Values</mat-label>
<mat-chip-grid #chipGrid aria-label="Enter values" formArrayName="values" [errorStateMatcher]="errorStateMatcher">
<mat-chip-row
*ngFor="let valueControl of form.controls.values.controls; let valueIndex = index"
(removed)="removeValue(form.controls.values, valueIndex)" [editable]="true">
{{ valueControl.value }}
<button matChipRemove [attr.aria-label]="'remove ' + valueControl.value">
<mat-icon>cancel</mat-icon>
</button>
</mat-chip-row>
<input class="form-control" placeholder="New value..." [matChipInputFor]="chipGrid"
[matChipInputSeparatorKeyCodes]="separatorKeysCodes" [matChipInputAddOnBlur]="true"
(matChipInputTokenEnd)="addValue(form.controls.values, $event)" required/>
</mat-chip-grid>
<mat-error *ngIf="form.controls.values.invalid">At least one value is required</mat-error>
</mat-form-field>
我无法使错误出现,我尝试检查 FormArray 上的无效,这是真的,然后尝试使用被忽略并且永远不会运行的自定义 errorStateMatcher。
【问题讨论】: