本文介绍了Reactive Formbuilder不接受函数参数中的表单控件名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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
    });
}

这篇关于Reactive Formbuilder不接受函数参数中的表单控件名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 15:23