1

I have a form That is responsible for Creating survey. Each Survey has Questions and Each Question has options.

Survey can have many Questions.

Question can have Many options.

Therefore i need something like bellow screenshot.

enter image description here

2
  • do you want to dynamic input field inside form? Commented Jul 11, 2018 at 15:51
  • yes as you can see button add question will add new question input and button add option will add option to each question Commented Jul 11, 2018 at 19:20

1 Answer 1

1

Check this out. I am using FormGroupArray to create a dynamic Question / Option List.

// Component
import { Component, OnInit } from '@angular/core';
import { FormGroup, FormBuilder, FormArray } from '@angular/forms';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
  title = 'app';
  public view: any;
  surveyForm: FormGroup;
  surveyQuestions: FormArray;
  surveyOptions: any[] = [];
  formData: string;
  constructor(private formbuilder: FormBuilder)  {
  }
  ngOnInit() {
    this.surveyForm = this.formbuilder.group({
      survey: this.formbuilder.array([this.createSurveyQuestion()])
    });
  }

  createSurveyQuestion(): FormGroup {
    return this.formbuilder.group({
      question: [''],
      options: this.formbuilder.array([this.createOption()])
    });
  }
  createOption(): FormGroup {
    return this.formbuilder.group({
      option: ['']
    });
  }

  addQuestion(): void {
    this.surveyQuestions = this.surveyForm.get('survey') as FormArray;
    this.surveyQuestions.push(this.createSurveyQuestion());
  }
  addOption(indx: number): void {
    let questCtrl =  <FormArray>this.surveyForm.get('survey');
    let m = questCtrl.controls[indx];
    let opts = m.get('options') as FormArray;
    opts.push(this.createOption());
  }
  get getSurveyControls() {
    return <FormArray>this.surveyForm.get('survey');
  }
  getQuestionOptionControls(questIndex: number){
    let questCtrl =  <FormArray>this.surveyForm.get('survey');
    let m = questCtrl.controls[questIndex];
    let opts = <FormArray>m.get('options');
    return opts;
  }
  convertFormData() {
    this.formData = JSON.stringify(this.surveyForm.value);
  }
}

Template -

// Template 
<form [formGroup]="surveyForm">
    <button (click)="addQuestion()">Add Question</button>
  <div formArrayName="survey" *ngFor="let quest of getSurveyControls.controls; index as i;" [attr.data-index]="i">
    <div [formGroupName]="i">
      <input formControlName="question" placeholder="Question"/>
      <button (click)="addOption(i)">Add Option</button>
      <div formArrayName="options" *ngFor="let opt of getQuestionOptionControls(i).controls; index as oidx;"[attr.data-index]="oidx">
        <div [formGroupName]="oidx">
          <input formControlName="option" placeholder="option"/>
        </div>
      </div>
    </div>
  </div>
</form>

<pre> {{surveyForm.value | json }}</pre>
Sign up to request clarification or add additional context in comments.

2 Comments

ERROR Error: Cannot find control with path: 'questions -> options -> 0'
can you create a plunk or something - so I can see whats going on?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.