在FormControl中更新验证器 [英] Update Validators in FormControl

查看:102
本文介绍了在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))

推荐答案

如果以后要设置新的Validator,则可以使用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.controls.input.setValidators([Validators.required, 
                                          Validators.minLength(4)]);

this.myForm.controls.input.updateValueAndValidity();

演示

如果要更新字段值,可以使用patchValue.

And if you want to update the field value, you can as mentioned use patchValue.

这篇关于在FormControl中更新验证器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆