这是我的html代码:
<form class="form justify-content-center" [formGroup]="checkInForm">
<input type="number" class="form-control d-inline w-auto m-3" formControlName="checkInCode" />
<div *ngIf="!checkInForm.valid && (checkInForm.controls.checkInCode.dirty || checkInForm.controls.checkInCode.touched)">
<h6 *ngIf="checkInForm.controls.checkInCode.errors.serverSide" class="text-danger col-12">Some messages</h6>
</div>
</form>
这是我的ts:
checkInForm = this.fb.group({
checkInCode: new FormControl('', [Validators.required, Validators.min(1000), Validators.max(9999)])
});
validateFn()
{
this.checkInForm.controls.checkInCode.markAsTouched();
this.checkInForm.controls.checkInCode.setErrors({ "serverSide": true });
}
我试图调试它。当我调用setErrors时,它运行正常(checkInCode.valid为INVALID,错误的serverSide为true),但是每当我退出validate函数时,checkInCode.errors为null。