我有一个看起来像这样的反应形式。
@Component({
selector: 'app-add-problem',
templateUrl: './add-problem.component.html',
styleUrls: ['./add-problem.component.css']
})
export class AddProblemComponent implements OnInit {
public addProblemForm:FormGroup;
@Input() baseImageUrl;
@Input() topicId;
@Input() topicName="Must Select a topic to proceed";
constructor(private _fb:FormBuilder) { }
ngOnInit() {
this.addProblemForm=this._fb.group({
Name:['',[Validators.required,Validators.minLength(5)]],
TopicId:[5,[Validators.required]],
Order:['',Validators.required],
Description:[''],
QuestionTag:[''],
Type:['',Validators.required],
BaseImageUrl:[this.baseImageUrl,Validators.required],
ProblemJson: this._fb.group({
QuestionText:this._fb.array([]),
Option1Text:this._fb.array([]),
Option2Text:this._fb.array([]),
Option3Text:this._fb.array([]),
Option4Text:this._fb.array([]),
SolutionText:this._fb.array([]),
CorrectOption:this._fb.group(
{
A:[false],
B:[false],
C:[false],
D:[false]
}
)
})
});
this.addProblemJsonControls();
}
initQuestionParts(){
return this._fb.group({
Type: ['', Validators.required],
Text:['',Validators.required]
})
}
addProblemJsonControls(){
const control1 = <FormArray>this.addProblemForm.controls['ProblemJson']['QuestionText'];
control1.push(this.initQuestionParts());
const control2 = <FormArray>this.addProblemForm.controls['ProblemJson']['Option1Text'];
control2.push(this.initQuestionParts());
const control3 = <FormArray>this.addProblemForm.controls['ProblemJson']['Option2Text'];
control3.push(this.initQuestionParts());
const control4 = <FormArray>this.addProblemForm.controls['ProblemJson']['Option3Text'];
control4.push(this.initQuestionParts());
const control5 = <FormArray>this.addProblemForm.controls['ProblemJson']['Option4Text'];
control5.push(this.initQuestionParts());
const control6 = <FormArray>this.addProblemForm.controls['ProblemJson']['SolutionText'];
control6.push(this.initQuestionParts());
}
在 addProblemJsonControls() 方法中,我试图访问 ProblemJson Form Group 的不同控件,但我收到一条错误消息
EXCEPTION: Uncaught (in promise): Error: Error in :0:0 caused by: Cannot read property 'push' of undefined
Error: Error in :0:0 caused by: Cannot read property 'push' of undefined
请帮助我正确访问 FormGroup。我是反应形式的新手。