动态addControl到Formgroup Angular 5 [英] dynamically addControl to formgroup Angular 5

查看:345
本文介绍了动态addControl到Formgroup Angular 5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试向Angular中的formGroup动态添加新的formControl条目.

Trying to dynamically add a new formControl entry to my formGroup in Angular.

method() {
  this.testForm.addControl('new', ('', Validators.required));
}

可以做到吗?

推荐答案

可以,但是第二个参数应该是FormControl实例.像这样:

Sure, but the second parameters should be a FormControl instance. Something like:

this.testForm.addControl('new', new FormControl('', Validators.required));

如果需要,还可以使用'setValidators'方法动态添加验证器.

you can also add the validators dynamically if you want with the 'setValidators' method.

此处的信息: https://angular.io/api/forms/FormGroup#addControl

这篇关于动态addControl到Formgroup Angular 5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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