I have a JSON response coming back from the server that is an assessment object with an array of questions. I need to path the values to my reactive form. This patch value function is working when there is only 1 question:
this.caseAssessmentForm.patchValue({
ID: assessment.templateID,
name: assessment.name,
type: assessment.typeName,
status: "Not Started",
description: assessment.description,
questions: {
questionScore: '',
questionAnswer: '',
questionGoal: assessment.templateQuestions[0].goal,
questionName: assessment.templateQuestions[0].name
}
});
The problem is that once there are multiple questions returning, I can't patch the value as an array. I've tried using questions: this.FormBuiler.array([]), but I still can't figure out how to dynamically patch the values. Does anyone have any ideas?
What you need to do is to iterate the incoming array, push each object as a FormGroup to an formArray. So the build could have an empty form array:
...where
fb
is referring toFormBuilder
.When you get your data, you iterate it and push formgroups to that formarray
questions
, so: