响应式表单生成器不接受来自功能参数的表单控件名称 [英] Reactive formbuilder doesn't accept form control name from function's parameter

查看:52
本文介绍了响应式表单生成器不接受来自功能参数的表单控件名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有创建类似表格组的功能,

I have a function to create form group like,

public initFormGroup(control_name) {
        console.log(control_name) // can log control_name
        return this._formBuilder.group({
            control_name: '' // This control_name is not same as its function property
        });
    }

我正在通过功能参数传递控件名称,但是this._formBuilder.group中的control_name与我传递的名称不同

I am passing the control name through the function parameter but the control_name inside this._formBuilder.group is not as that i passed

结果终于看起来像,

"children": [
      {
        "control_name": ""
      },
      {
        "control_name": ""
      }
    ]

所需的输出应该是

"children": [
      {
        "programming": ""
      },
      {
        "networking": ""
      }
    ]

推荐答案

您需要使用方括号表示法获取参数值:

You need to use bracket notation to get the value of your parameter:

public initFormGroup(control_name) {
    console.log(control_name) // can log control_name
    return this._formBuilder.group({
        [control_name]: '' // now control will have the value of your parameter
    });
}

这篇关于响应式表单生成器不接受来自功能参数的表单控件名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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