我在ngOnInit()
我想要做的是在ngOnInit()
的末尾手动设置一些表单控件。
ngOnInit(){
...Form creation...
this.parsedJson.sections[0].form.controls['clientName'].setValue("John");
}
它不会以这种方式更新控件,如果我手动触发相同的操作(例如:按钮点击),它将进行更新。
this.parsedJson.sections[0].form.controls['clientName'].setValue("John");
我也尝试使用patchValue
代替setValue
,但结果是一样的。
表单创建:
for(let section of this.parsedJson2.sections){
section.form = new FormGroup({});
if(section.section_type == 'normal') {
for (let question of section.question_list) {
if (question.qtype == 'input-text') {
const control: FormControl = new FormControl(question.answer, Validators.required);
section.form.addControl(question.key, control);
}
if (question.qtype == 'input-email') {
const control: FormControl = new FormControl(question.answer, Validators.email);
section.form.addControl(question.key, control);
}
if (question.qtype == 'multi-choice') {
for (let choice of question.qchoices) {
const control: FormControl = new FormControl(choice.selected, Validators.required);
section.form.addControl(choice.key, control);
}
}
else {
const control: FormControl = new FormControl(question.answer, Validators.required);
section.form.addControl(question.key, control);
}
}
}
}
答案 0 :(得分:1)