我使用ControlValueAccessor实现了基于官方指南的自定义控制
验证错误在我们触及自定义字段之前不会显示的问题。在我的例子中,我对步进器有一个问题,但是当我执行this.form.markAllAsTouched()时,它也不起作用。

我提供下面的例子,基于官方的定制电话字段。使用空字段单击"Next“。
发布于 2022-10-24 10:24:19
请参见函数errroState。您可以添加条件ngControl.invalid并标记为ngControl是使用
get errorState(): boolean {
const touched=!!this.ngControl?.touched;
if (touched!=this.touched)
{
this.touched=touched;
this.stateChanges.next();
}
return (this.parts.invalid || !!this.ngControl?.invalid) && this.touched;
}然后,如果标记为触碰的FormControl,则会看到错误
<button type="button" mat-button matStepperNext
(click)="form.markAllAsTouched()" >Next</button>发布于 2022-10-24 08:06:33
您只需将MyTelInput属性touched从false更改为true即可。
export class MyTelInput
implements ControlValueAccessor, MatFormFieldControl<MyTel>, OnDestroy
{
.
.
.
touched = true; //change from false to true
.
.
}也可以在角度材料文档中查看更多信息。
https://material.angular.io/guide/creating-a-custom-form-field-control#errorstate
https://stackoverflow.com/questions/74108591
复制相似问题