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

问题描述 投票:1回答:1

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

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": ""
      }
    ]
angular angular-cli
1个回答
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
    });
}
© www.soinside.com 2019 - 2024. All rights reserved.