Search code examples
angularformarray

how to use insert function in FormArray?


I am tying to insert data by index it's not working i have sequence this sequence use as index but not patch properly

angualr.ts

for (let key in this.existingCompanyObj) {
              companyControl.insert(Number(this.existingCompanyObj[key].sequence), this.fb.group({ company: this.companyNamesObj[key], role: this.existingCompanyObj[key].role, sequence: this.existingCompanyObj[key].sequence }));


[0:{company:company[0],role:role[0]},1:{company:company[1],role:role[1]};

Solution

  • insert function not working properly use two for loop for that

    for (let key in this.existingCompanyObj)
    {    
     companyControl.push(new FormControl());
    }
    for (let key in this.existingCompanyObj
    {
     companyControl.setControl(this.existingCompanyObj[key].sequence, this.fb.group({ company: 
     this.companyNamesObj[key], role: this.existingCompanyObj[key].role, sequence: 
     this.existingCompanyObj[key].sequence }));
    }
    

    first you need to push(new FormControl) blank formControl and then a sing value using setControl(index object)