【问题标题】:How do I set a child formGroup in a formGroup without the initializer?如何在没有初始化程序的情况下在 formGroup 中设置子 formGroup?
【发布时间】:2023-04-08 23:47:01
【问题描述】:

我正在尝试将我的 formGroup 中的属性动态设置为 formGroup;如果你愿意的话,一个子表单组

// Example 1
var parent = this._formBuilder.group({
    id; [''].
    child: this._formBuilder.group({
        name: ['']
    })
})

 

// Example 2
var parent = this._formBuilder.group({
    id; [''],
    child: undefined
})

 

parent.patchValue({'child': this._formBuilder.group({ name: [''] }) })

示例 1 将使 parent.get('child') 返回 formGroup。然而,在选项 2 中,parent.get('child').value 返回表单组。

如何让动态设置的 formGroup 像在 formGroup 初始化程序中设置一样工作?

【问题讨论】:

    标签: angular formgroups


    【解决方案1】:

    patchValue 不是添加表单组或表单控件,而是更新 表单值。

    要动态添加表单组,需要使用setControladdControl

    parent.setControl('child', this._formBuilder.group({ name: [''] }))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-09
      • 1970-01-01
      • 1970-01-01
      • 2017-11-02
      • 1970-01-01
      • 2021-09-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多