Search code examples
angularangular6form-control

Angular6 - ngIf does not destroy object


I have a parent component that will change the child's form that is passed as @Input() when a button is clicked. I use ngIf to render the child component, but when I click to change the form, the child component does not get destroyed and recreated.

parent.component.ts

form: FormGroup;
showChildForm: boolean;
num: number;

ngOnInit(){ 
   this.showChildForm = false; 
   this.num = 1;
   this.form = new FormGroup({group:{'name', new FormControl('name'+this.num,[])}})
}

changeForm(){
   this.num += 1;
   this.showChildForm = true; 
   this.form = new FormGroup({group:{'name', new FormControl('name'+this.num,[])}})
}

parent.component.html

<button (click)="changeForm()"></button>
<child *ngIf="showChildForm" [form]="form"></child>

child.component.ts

@Input() form: FormGroup;

child.component.html

<form [formGroup]="form">
   <input type="text" [formControl]="form.get('name')"/>
</form>

Solution

  • Inside changeForm you are not setting this.showChildForm to false again.

    Try doing this:

    changeForm(){
      this.num += 1;
      this.showChildForm = false; 
      setTimeout(() => {
        this.showChildForm = true; 
        this.form = new FormGroup({group:{'name', new FormControl('name'+this.num,[])}})
      })
    }
    

    toggling it off and then back on again in the next tick cycle (using setTimeout) will cause the component to be destroyed and recreated.