Reactive formbuilder不接受函数参数中的表单控件名称

时间:2017-12-19 14:25:22

标签: angular angular-cli

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

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不是我传递的

结果终于看起来像,

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

所需的输出应为

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

1 个答案:

答案 0 :(得分:1)

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

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