1

I want dynamically remove components from my parent component.

<div>

 <my-component #component
    (remove)="onRemove(0)">
 </my-component>

 <my-component #component
    (remove)="onRemove(1)">
 </my-component>

 <my-component #component
    (remove)="onRemove(2)">
 </my-component>
</div>

child component emits to the parent, it works, in onRemove function:

constructor(private generalViewContainerRef: ViewContainerRef) {  
}

private onRemoveWidget(index:number) {

     this.generalViewContainerRef.remove(component);
}

but it's not working.

how can I do it?

Sh. Pavel
  • 1,584
  • 15
  • 28
cucuru
  • 3,456
  • 8
  • 40
  • 74

1 Answers1

2

You can remove and add dynamically using *ngIf

HTML:

<div>
 <my-component *ngIf="showComponent">
 </my-component>
</div>

TS:

showComponent: boolean = false
Akj
  • 7,038
  • 3
  • 28
  • 40