如何在FormArray中预设响应式表单的值?



我有一个带有动态字段的响应式表单。我试图建立一个CRUD操作。在"Create"是工作很好,但要更新的形式,我需要预填充字段。我可以预先填充不在&;formarray &;中的字段但是由于我正在使用一些动态字段,所以我也想在动态字段中显示数据。

这是我的样本有效载荷,我必须在表单中预填充:'

scriptRunnerData: any = {
"workflow_name": "vvvv",
"runnerNodes": [
{
"script_name": "sample.py",
"script_file_content": "jhsvdfhskjdhfvksdj fksjd"
},
{
"script_name": "sample1.py",
"script_file_content": "sdfjhdsbf sjd fsjd sd dfsb dlfsdjf sd  fls"
}
],
"executer_command": "dfhdhdjhkdfkdhfg.py",
"config_name": "config.json",
"config_file_content": "{"name":"vvvv"}"
}

I am passing modes 'create' and 'edit'. Create is working fine but for View I am getting error:ERROR: Cannot find control with path: 'runnerNodes ->0→script_name'and错误错误:无法找到路径:'runnerNodes ->0→script_file_content '

下面是我的代码:'
createWorkflowAddFormBuilderObj(){
if(this.data.mode === 'create'){
console.log("data", this.data);
this.workflowAddForm = this.formBuilder.group({
workflow_name: '',
runnerNodes: this.formBuilder.array([ this.createRunnerNode() ]),
executer_command: '',
config_name: '',
config_file_content: ''
});
}else if(this.data.mode === 'edit'){
this.workflowAddForm = this.formBuilder.group({
workflow_name: [{value: this.scriptRunnerData.workflow_name, disabled: false}],
runnerNodes: this.formBuilder.array([ this.createRunnerNode() ]),
executer_command: [{value: this.scriptRunnerData.executer_command, disabled: false}],
config_name: [{value: this.scriptRunnerData.config_name, disabled: false}],
config_file_content: [{value: this.scriptRunnerData.config_file_content, disabled: false}]
});
}

}
createRunnerNode(): FormGroup {
if(this.data.mode === 'create'){
return this.formBuilder.group({
script_name: '',
script_file_content: ['', Validators.required]
});
}else if(this.data.mode === 'edit'){
this.scriptRunnerData.runnerNodes.forEach(node =>{
return this.formBuilder.group({
script_name: [{value: node.script_name, disabled: false}],
script_file_content: [{value: node.script_file_content, disabled: false}, Validators.required]
});
})
}
}
addMoreRunner(){
this.runnerNodes = this.workflowAddForm.get('runnerNodes') as FormArray;
this.runnerNodes.push(this.createRunnerNode());
this.runnerNodeLength = this.runnerNodes.controls.length;
}

Can anyone please suggest what exactly I have to do. IssetValueORpatchValue '将在这里工作。如何在此场景中使用这些方法。提前谢谢。

我已经修改了createWorkflowAddFormBuilderObj()

createWorkflowAddFormBuilderObj(){
if(this.data.mode === 'create'){
console.log("data", this.data);
this.workflowAddForm = this.formBuilder.group({
workflow_name: '',
runnerNodes: this.formBuilder.array([ this.createRunnerNode() ]),
executer_command: '',
config_name: '',
config_file_content: ''
});
}else if(this.data.mode === 'edit'){
const controls = this.scriptRunnerData.runnerNodes.map((c) =>
this.formBuilder.group({
script_name: [{value: c.script_name, disabled: true}],
script_file_content: [{value: c.script_file_content, disabled: true}]
})
);
this.workflowAddForm = this.formBuilder.group({
workflow_name: [{value: this.scriptRunnerData.workflow_name, disabled: true}],
runnerNodes: this.formBuilder.array(controls),
executer_command: [{value: this.scriptRunnerData.executer_command, disabled: true}],
config_name: [{value: this.scriptRunnerData.config_name, disabled: true}],
config_file_content: [{value: this.scriptRunnerData.config_file_content, disabled: true}]
});
}
}

现在运行正常

相关内容

  • 没有找到相关文章

最新更新