问题
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