javascript - 如何在 Angular Reactive Form 的嵌套数组字段中构建字段数组?

标签 javascript angular typescript angular-reactive-forms

我有一个表格负责创建调查。 每个调查都有问题,每个问题都有选项。

调查可以有很多问题。

问题可以有很多选项。

因此我需要类似下面截图的东西。

enter image description here

最佳答案

检查一下。我正在使用 FormGroupArray 创建动态问题/选项列表。

// 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 
<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>

关于javascript - 如何在 Angular Reactive Form 的嵌套数组字段中构建字段数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51289265/

相关文章:

Javascript OOP - 从同一类的不同方法调用方法

javascript - 是否可以在 JQuery 中执行 ".value +="?

javascript - 如何将背景图像添加到签名板

javascript - router.navigate 与查询参数 Angular 5

angular - 使用 Angular Material 从 Angular 2 中的垫选择列表中删除选定的行

javascript - Typescript 禁用特定 ID 内具有类的所有元素

javascript - 访问 parent 的另一个 child

javascript - Angular 2+ 拦截 Command+S 的优雅方式

javascript - 如何更改传单中的标记颜色?

reactjs - useSelector() 和 useDispatch() 是否替换了 mapStateToProps() 和 mapDispatchToProps()?