我有一个创建表单组的功能,
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": ""
}
]
您需要使用括号表示法来获取参数的值:
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
});
}