Reactive formbuilder doesn't accept form control name from function's parameter

南楼画角 提交于 2020-01-03 01:34: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
        });
    }

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

The result finally looked like ,

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

The desired output should be,

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

回答1:


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


来源:https://stackoverflow.com/questions/47889068/reactive-formbuilder-doesnt-accept-form-control-name-from-functions-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!