反应式表单构建器不接受来自函数参数的表单控件名称 [英] Reactive formbuilder doesn't accept form control name from function's parameter

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

问题描述

我有一个创建表单组的功能,如

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天全站免登陆