响应式表单 setValidator 和 updateOn

Cor*_*row 6 angular angular-reactive-forms

我试图在 formControl 上动态设置验证器,但似乎无法让 updateOn: "blur" 正常工作。它似乎不是在检查模糊,而是在检查变化。我需要做一些特别的事情才能让它在模糊而不是变化上工作吗?

this.form.get("deviceInfo").controls.deviceNumber.setValidators({validators: [Validators.required, this.checkDeviceExists()], updateOn: 'blur'});
Run Code Online (Sandbox Code Playgroud)

添加了 updateValueAndValidity() 但仍然会在更改时触发而不是模糊

var deviceInfo = this.form.get("deviceInfo");

deviceInfo.controls.deviceNumber.setValidators({validators: [Validators.required, this.checkDeviceExists()], updateOn: 'blur'});
deviceInfo.controls.deviceNumber.updateValueAndValidity();
Run Code Online (Sandbox Code Playgroud)

Fra*_*ous 1

显示动态验证的示例

this.form.get('company_name').setValidators(Validators.compose([Validators.required, Validators.maxLength(45)]));      


this.form.updateValueAndValidity();
Run Code Online (Sandbox Code Playgroud)

  • 为什么不在表单初始化时设置 onBlur 属性并动态应用验证。?@CoreyWitherow (3认同)