2
votes

I've got an Angular component which displays data from a FormArray but there is also another FormGroup which only becomes visible when a button is clicked.

When the component loads, if I click on the button to make the other form visible immediately then it works. However if I click on another button or one of the FormArray inputs first when I make the other form visible it will error with 'Cannot find control'. Clicking to close and then re-display the other form will then work correctly.

I've spent hours trying to track down what's going wrong and it only seems to be when there is an *ngFor to loop through the FormArray items. I've boiled it down to this example:

import { Component, OnInit } from '@angular/core';
import { FormGroup, FormControl, FormArray } from '@angular/forms';

@Component({
  selector: 'app-test-filter-component',
  templateUrl: './test-filter-component.component.html',
  styleUrls: ['./test-filter-component.component.scss']
})
export class TestFilterComponentComponent implements OnInit {

  public testform: FormGroup;
  public otherForm: FormGroup;
  public otherFormVisible = false;

  constructor() {}

  get orders() {
    return this.testform.get('orders') as FormArray;
  }

  anotherClick() {}

  ngOnInit() {
    this.testform = new FormGroup({
      orders: new FormArray([])
    });
    this.otherForm = new FormGroup({
      test: new FormControl('testvalue')
    });
    for(let idx = 0; idx < 50; idx++) {
      this.orders.push(new FormGroup({id: new FormControl(idx), name: new FormControl('test')}));
    }
  }
}
<div *ngIf="otherFormVisible">
    <form [formGroup]="otherForm">
        <input formControlName="test">
    </form>
</div>
<button class="btn btn-primary" (click)="otherFormVisible = !otherFormVisible">other form</button>
<button class="btn btn-primary" (click)="anotherClick()">Click here first</button>
<form [formGroup]="testform">
    TEST CONTROL
    <div formArrayName="orders" *ngFor="let order of orders.controls; let i = index">
        <div [formGroupName]="i">
            <input formControlName="id">
            <input formControlName="name">
        </div>
    </div>
</form>

If you click straight on 'other form' it shows the other form correctly, but if you click on 'Click here first' or any of the other inputs first it will error saying:

ERROR Error: Cannot find control with name: 'test'

If anyone knows how to get this working properly it would save me from hours more frustration.

3
Not able to reproduce the issue given from your Typescript code.My guess you are missing anotherClick() implementation code. Could you please provideSats
It is reproducible: stackblitz.com/edit/…Poul Kruijt

3 Answers

1
votes

You can replace

<div *ngIf="otherFormVisible">

with

<div [hidden]="!otherFormVisible">
1
votes

Agree with @Stratubas answer and thanks to him for pointing out issue.

This issue is not reproducible in chrome version is 79.0.3945.130 (Official Build) (64-bit).

But i tried the same issue in stackblitz link given by @PierreDuc, in Microsoft Edge, that had Version 80.0.361.48 (Official build) (64-bit), This issue was reproducible here. This browser is Microsoft's Chromium-based browser.

I hope this will be helpful for anyone in future.

1
votes

While the accepted answer did fix the small example I posted, the problem quickly reappeared when my forms got more complex.

After some further searching I discovered it is in fact a bug with Chrome 80+ https://github.com/angular/angular/issues/35214

And there is a workaround which fixed my problems here: https://github.com/angular/angular/issues/35219#issuecomment-583425191