有没有办法在声明后更新控件,比如
this.input = new FormControl('', Validators.required)
this.form = this.formBuilder.group({
input = this.input
})
this.input.update('', Validators.maxlength(20))
最佳答案
如果您想稍后设置新的验证器,您可以使用 setValidators
,您可能还想更新值和有效性,它可以使用 运行更新值和有效性
。这是一个简单的例子:
this.myForm.get('input').setValidators([Validators.required,
Validators.minLength(4)]);
this.myForm.get('input').updateValueAndValidity();
如果你想更新字段值,你可以使用patchValue
。
关于angular - 更新 FormControl 中的验证器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43577218/