How do I clear the FormArray in Angular Reactive Forms
Asked Answered
G

2

8

I am doing resetting of form. It resets the whole form but except FormArray.

Creating the form and declaring formArray within it

createForm(){
    this.invoiceForm = this.formBuilder.group({
      'name': ['', Validators.required],
      'gst': [''],
      'currency': [''],
      'addressLine1': ['', Validators.required],
      'addressLine2': [''],
      'city': ['', Validators.required],
      'state': ['', Validators.required],
      'country': ['', Validators.required],
      'postalCode': ['', Validators.required],
      'email': ['', [Validators.required, Validators.email]],
      'invoiceparticulars': this.formBuilder.array([]),
      'isGstshidden' : true
    });

  }

Trying to modify details of the form from the incoming data by resetting the data even though I called reset() function formArray retaining previous entries in it.

 modifyInvoice(index){

   this.invoiceForm.reset();

   let modifyData = this.modifyInvoiceArray[index];
   console.log(modifyData);

   this.invoiceNumber = modifyData.invoiceNumber;
   this.invoiceForm.patchValue({name: modifyData.address.Name});
   this.invoiceForm.patchValue({email: modifyData.email});
   this.invoiceForm.patchValue({gst: modifyData.GSTnumber});
   this.invoiceForm.patchValue({addressLine1: modifyData.address.AddressLine1});
   this.invoiceForm.patchValue({addressLine2: modifyData.address.AddressLine2});
   this.invoiceForm.patchValue({city: modifyData.address.City});
   this.invoiceForm.patchValue({country: modifyData.address.Country});
   this.invoiceForm.patchValue({postalCode: modifyData.address.PostalCode});
   this.invoiceForm.patchValue({state: modifyData.address.State});
   console.log(modifyData['particulars']);
}
Gilbertgilberta answered 3/5, 2018 at 10:12 Comment(1)
Can you show your code?Isolationist
I
6

Try to add this code

const control = <FormArray>this.invoiceForm.controls['invoiceparticulars'];
        for(let i = control.length-1; i >= 0; i--) {
            control.removeAt(i)
    }
Isolationist answered 7/5, 2018 at 2:44 Comment(5)
rather than this I can even directly call this.createForm(); which resets the form right?!Gilbertgilberta
@DhaanappagoudaPatil. I'm sorry i cant understand what youre saying. But is my solution working?Isolationist
This looks unreadable. Why not a simple while (arr.length > 0) { arr.removeAt(0) } ?Parthen
thank,s that exactly what i am looking for thank,s once againPrologize
Instead of array.clear this is more productive. As I want the length atleast to be 1. So I start the loop with Index 1 and use removeAt method to clear except item in index 0Gomez
K
29

Angular 8

simply use clear() method on formArrays :

(this.invoiceForm.controls['invoiceparticulars']).clear();

OR :

let frmArray = this.invoiceForm.get('invoiceparticulars') as FormArray;
frmArray.clear();
Kenzie answered 31/7, 2019 at 21:17 Comment(0)
I
6

Try to add this code

const control = <FormArray>this.invoiceForm.controls['invoiceparticulars'];
        for(let i = control.length-1; i >= 0; i--) {
            control.removeAt(i)
    }
Isolationist answered 7/5, 2018 at 2:44 Comment(5)
rather than this I can even directly call this.createForm(); which resets the form right?!Gilbertgilberta
@DhaanappagoudaPatil. I'm sorry i cant understand what youre saying. But is my solution working?Isolationist
This looks unreadable. Why not a simple while (arr.length > 0) { arr.removeAt(0) } ?Parthen
thank,s that exactly what i am looking for thank,s once againPrologize
Instead of array.clear this is more productive. As I want the length atleast to be 1. So I start the loop with Index 1 and use removeAt method to clear except item in index 0Gomez

© 2022 - 2024 — McMap. All rights reserved.