简体   繁体   中英

ERROR Error: formArrayName must be used with a parent formGroup directive

I'm using reactive-form-array for one of my projects. I'm trying to make a dynamic form which add/delete fields on button click. I'm getting the following error in my code

ERROR Error: formArrayName must be used with a parent formGroup directive. You'll want to add a formGroup directive and pass it an existing FormGroup instance

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


 public communicationForm: FormGroup;
  public items: FormArray;

  createItem(): FormGroup {
    return this.fb.group({
      communicationType: [''],
      communicationValue: ['']
    });
  }

  addItem(): void {
    // this.items = this.communicationForm.get('items') as FormArray;
    this.items.push(this.createItem());
  }

  constructor(private fb: FormBuilder) { }

  ngOnInit() {
    this.communicationForm = this.fb.group({
      items: this.fb.array([this.createItem()])
    }),
    this.items = this.communicationForm.get('items') as FormArray;
  }


<div class="col-md-6">
        <h3>Communications</h3>
        <div formArrayName="items" *ngFor="let item of communicationForm.get('items').controls; let i = index;">
            <div [formGroupName]="i">
                <select class="form-group col-md-5" (change)="changeCommunication($event)" formControlName="communicationType">
                    <option value="" disabled>Communications</option>
                    <option *ngFor="let communication of Communications" [ngValue]="communication.id">
                        {{communication.name}}
                    </option>
                </select>
            </div>
            <div [formGroupName]="i">
                <input type="text" class="col-md-6" formControlName="communicationValue">
            </div>
            <div [formGroupName]="i">
                <i class="fa fa-plus-circle" (click)="addItem()"></i>
            </div>
        </div>
    </div>

Add a parent formGroup:

<div class="col-md-6" [formGroup]="communicationForm">

Only other thing I would change is add a getter for the items:

TS

get items(): FormArray {
  return this.communicationForm.get('items') as FormArray;
}

And ngOnInit is:

ngOnInit() {
  this.communicationForm = this.fb.group({
    items: this.fb.array([this.createItem()])
  })
}

HTML

<div class="col-md-6">
        <h3>Communications</h3>
        <div formArrayName="items" *ngFor="let item of items.controls; let i = index;">
            <div [formGroupName]="i">
                <select class="form-group col-md-5" (change)="changeCommunication($event)" formControlName="communicationType">
                    <option value="" disabled>Communications</option>
                    <option *ngFor="let communication of Communications" [ngValue]="communication.id">
                        {{communication.name}}
                    </option>
                </select>
            </div>
            <div [formGroupName]="i">
                <input type="text" class="col-md-6" formControlName="communicationValue">
            </div>
            <div>
                <i class="fa fa-plus-circle" (click)="addItem()"></i>
            </div>
        </div>
    </div>

when we has a FormArray NOT closed in a formGroup we use

<form [formGroup]="items">
   <!--see that iterate over items.controls-->
   <!-- and create a formGroup= the variable -->
   <div *ngFor="let group of items.controls" [formGroup]="group">
      <input formControlName="communicationType">
      <input formControlName="communicationValue">
   </div>
</form>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM