typescript 如何在Angular中从FormArray获取数据

inb24sb2  于 7个月前  发布在  TypeScript
关注(0)|答案(1)|浏览(75)

我想定义一个有答案的问题。下面是我的代码:

formGroup!:FormGroup;
  answerFormGroup:FormGroup=this.formBuilder.group({
    text: ['d', Validators.required],
    value: [0, Validators.required]
  });
  ngOnInit() {
    this.formGroup = this.formBuilder.group({
      question:['',Validators.required],
      questionType: [1,Validators.required],
      answers: new FormArray([])
    });
   this.questionTypes=this.localDataService.questionType;
   this.addAnswer();
  }

   // convenience getters for easy access to form fields
   get answers() {
    return this.formGroup.controls["answers"] as FormArray;
  }

  addAnswer(){
    this.answers.push(this.formBuilder.group({
      text: ['d', Validators.required],
      value: [0]
    }));
  }

  submit(){
       console.log(JSON.stringify(this.formGroup.value));
  }

字符串
但是当我提交表单时,答案值是默认值,它不会改变。在表单中,我将默认值“d”改为“b”,它仍然是“d”。下面是结果:

{
  "question": "question1",
  "questionType": 1,
  "answers": [
    {
      "text": "d",
      "value": 0
    }
  ]
}


下面是模板:

<form class="question-card" [formGroup]="formGroup">
    <mat-form-field>
        <mat-label>Question</mat-label>
        <input matInput formControlName="question" placeholder="Question">
    </mat-form-field>
    <mat-form-field>
        <mat-label>Type</mat-label>
        <mat-select formControlName="questionTypeControl" (selectionChange)="updateQuestionType($event)">
            @for (item of questionTypes; track item) {
                <mat-option [value]="item.id">
                    <mat-icon>{{item.icon}}</mat-icon> {{item.text}}
                </mat-option>
            }
        </mat-select>
    </mat-form-field>  
    <div [formGroup]="answerFormGroup">
    @for(item of answers.controls; track item){
        <mat-form-field>
            <mat-label>Value</mat-label>
            <input matInput type="text" formControlName="value">
        </mat-form-field>
        <mat-icon class="delete-btn" (click)="deleteAnswer($event)">delete_forever</mat-icon>
        <mat-form-field>
            <mat-label>Text</mat-label>
            <input matInput type="text" formControlName="text">
        </mat-form-field>
    }
    </div>
    <button mat-mini-fab (click)="addAnswer()">
            <mat-icon>add</mat-icon>
    </button>
    <button type="button" (click)="submit()">Submit</button>
</form>

ffscu2ro

ffscu2ro1#

您正在更新answerFormGroup FormGroup中的text控件,而不是formGroup。从此处开始:

<div [formGroup]="answerFormGroup">
  ...
</div>

字符串
formGroup模板中,您需要为answers提供formArray控件。

<form class="question-card" [formGroup]="formGroup">

  ...

  <ng-container formArrayName="answers">
    @for (item of answers.controls; track item; let i = $index) {

      <div [formGroupName]="i">
        <mat-form-field>
          <mat-label>Value</mat-label>
          <input matInput type="text" formControlName="value" />
        </mat-form-field>
        <mat-icon class="delete-btn" (click)="deleteAnswer($event)"
          >delete_forever</mat-icon
        >
        <mat-form-field>
          <mat-label>Text</mat-label>
          <input matInput type="text" formControlName="text" />
        </mat-form-field>
      </div>
    }
  </ng-container>

  ...

</form>


@for语法是Angular 17中的一个新特性,对我来说很新鲜。如果你正在寻找旧的方法:

<ng-container formArrayName="answers">
  <ng-container *ngFor="let answer of answers.controls; let i = index">
    <div [formGroupName]="i"> 
    
    <mat-form-field>
      <mat-label>Value</mat-label>
      <input matInput type="text" formControlName="value" />
    </mat-form-field>
    <mat-icon class="delete-btn" (click)="deleteAnswer($event)"
      >delete_forever</mat-icon
    >
    <mat-form-field>
      <mat-label>Text</mat-label>
      <input matInput type="text" formControlName="text" />
    </mat-form-field>
  </div>
    
  </ng-container>
</ng-container>


Demo @ StackBlitz
请注意,您在formGroup中缺少questionTypeControl控件。

相关问题