Angular removeAt() 上输入的 4 表单数组值不正确

Angular 4 form array incorrect value in input on removeAt()

我创建了一个 Plunker 来演示这个问题

https://embed.plnkr.co/pgu7szf9ySwZSitOA5dq/

如果您删除 #2,您会看到 #5 在最后两个框中出现两次。我不明白为什么会这样。

您应该像这样将 FormArray 嵌套在 FormGroup 中:

export class AppComponent implements OnInit {
    public formG: FormGroup;
    public formArray: FormArray;

    constructor(private fb: FormBuilder) { }

    ngOnInit() {
      this.createForm(); 
    }


    createForm() {
      this.formArray = this.fb.array([
        this.fb.control(1),
        this.fb.control(2),
        this.fb.control(3),
        this.fb.control(4),
        this.fb.control(5),
      ]);
      this.formG = this.fb.group({
        farray: this.formArray
      });
      console.log(this.formArray);
    }

    addQuestion() {
      this.formArray.push(this.fb.control(''));
    }

    removeQuestion(i) {
      this.formArray.removeAt(i);
    }
}

和模板:

<div class="container" [formGroup]="formG">
  <div formArrayName="farray">
    <div class="row form-inline" *ngFor="let question of formArray.controls; let i = index">
      <textarea class="form-control" [formControlName]="i"></textarea>
      <button (click)="removeQuestion(i)" class="btn btn-secondary">Remove</button>
    </div>
  </div>
</div>
<button (click)="addQuestion()" class="btn btn-secondary">Add</button>

表格在行动:https://embed.plnkr.co/hJ0NMmzGezjMzWfYufaV/