如何将新的FormGroup或FormControl附加到窗体 [英] How to append new FormGroup or FormControl to form

查看:141
本文介绍了如何将新的FormGroup或FormControl附加到窗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用FormBuilder创建的Angular中具有以下form:

I have the following form in Angular created with FormBuilder:

constructor(private fb: FormBuilder) {
    this.myForm = fb.group({
        'name': ['', [Validators.required],
        'surname': ['', [Validators.required],
        'email': ['', [validateEmail]],
        'address': fb.group({
            'street': [''],
            'housenumber': [''],
            'postcode': ['']
        }, { validator: fullAddressValidator })
    });
}

是否存在以编程方式将FormControl或new FormGroup之类的新字段附加到myForm的方法?

Does exist a way to programmatically append new fields such as FormControl or new FormGroup to myForm ?

我的意思是,如果我想按需或在某些条件下添加新字段,如何将项目添加到在constructor中首次创建的相同表单中?

I mean if I want to add new fields on demand or on some conditions, how to add items to the same form that is created the first time in the constructor?

推荐答案

您可以使用 FormGroup 类#!#addControl-anchor"rel =" noreferrer>文档

You can use addControl method of FormGroup class as per documentation

因此您可以执行以下操作:

So you can do as below :

this.myForm.addControl('newcontrol',[]);

这篇关于如何将新的FormGroup或FormControl附加到窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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