[英]Angular Reactive Form : How to preset values ? (FormArray containing objects)
[英]How to preset values of reactive form in FormArray?
我有一个带有一些动态场的反应形式。 我正在尝试构建一个 CRUD 操作。 “创建”工作正常但要更新表单我需要预填充字段。 我可以预填充不在“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:
I am passing modes 'create' and 'edit'. Create is working fine but for View I am getting error:
错误错误:找不到路径控制:'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. Is
Can anyone please suggest what exactly I have to do. Is
setValue OR
patchValue` 将在这里工作。 如何在这种情况下使用这些方法。 提前致谢。
我修改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}]
});
}
}
现在它按预期工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.