更新 FormControl 中的验证器 [英] Update Validators in FormControl
本文介绍了更新 FormControl 中的验证器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法在声明之后更新控件,比如
is there any way to update control after it declared, like
this.input = new FormControl('', Validators.required)
this.form = this.formBuilder.group({
input = this.input
})
this.input.update('', Validators.maxlength(20))
推荐答案
如果您想在以后设置新的验证器,您可以使用 setValidators
,您可能还想更新值和有效性,它可以用 updateValueAndValidity
运行.这是一个简单的例子:
You can use setValidators
if you want to set new Validator(s) at a later point, you'd probably also want to update the value and validity, it can be run with updateValueAndValidity
. Here's a simple example:
this.myForm.get('input').setValidators([Validators.required,
Validators.minLength(4)]);
this.myForm.get('input').updateValueAndValidity();
如果你想更新字段值,你可以如上所述使用patchValue
.
And if you want to update the field value, you can as mentioned use patchValue
.
这篇关于更新 FormControl 中的验证器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文