[英]Accessing Nested FormControls in Reactive Form @angular2
I am having a reactive form which looks something like this . 我有一个看起来像这样的反应形式。
@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());
}
In addProblemJsonControls() method i am trying to access the different controls of ProblemJson Form Group, but i am getting an error which says 在addProblemJsonControls()方法我试图访问ProblemJson表单组的不同控件,但我收到一个错误,说
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
Please help me in accessing the FormGroup correctly. 请帮我正确访问FormGroup。 I am new to Reactive Form. 我是Reactive Form的新手。
I just resolved this myself after hours of frustration. 经过几个小时的挫折后,我才解决了这个问题。 A couple of ideas: 一些想法:
This example from the Angular docs really helped me. 来自Angular文档的这个例子对我有帮助。
Accessing controls: 访问控件:
this.addProblemForm = this._fb.group({
ProblemJson: this._fb.group({
QuestionText:this._fb.array([]),
Option1Text:this._fb.array([])
});
const control1 = <FormArray>this.addProblemForm.get('ProblemJson.QuestionText');
HTML side: You really need both the component TS and the HTML parts correct together, or else you could get errors. HTML方面:您确实需要将组件TS和HTML部件一起更正,否则您可能会收到错误。 You didn't post the HTML, but just to add to this, it would look something like: 你没有发布HTML,但只是添加到这个,它看起来像:
<form [formGroup]="addProblemForm">
<div formGroupName="ProblemJson">
<div formArrayName="QuestionText"></div>
<div formArrayName="Option1Text"></div>
</div>
</form>
The key here is the structure/nesting of the FormGroup must match exactly in TypeScript and HTML . 这里的关键是FormGroup的结构/嵌套必须完全匹配TypeScript和HTML 。 You may not care about "ProblemJson" FormGroup in the HTML for example, but you have to declare it. 例如,您可能不关心HTML中的“ProblemJson”FormGroup,但您必须声明它。 That was my issue, at least. 这至少是我的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.