I am trying to build a dynamic form. In my particular case, I need a form where user can define a question and add its options dynamically.
The question title is a text field where user enter the question. The option field is also a text field where user add options for the question. A user can add 2 or more options for a particular question.
I am using Formbuilder and have something like this:
this.form = fb.group({
title: ["", Validators.required],
options:
fb.group({
option1: new Control(''),
option2: new Control('')
.....
})
});
I also know we can add control to a form group using this.form.addControl, and remove control using this.form.removeControl.
I need to allow user to add/remove options, but I am note sure how to name these dynamically added options so that when I submit response, I have the below posted to the server:
{
title:"My test question",
options:{
"option 1","option 2","option 3",.......
}
}
Can anyone please guide? Any help would be highly appreciate.