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?

3
  • 1
    Can you quantify not working? Do you get an error? Or other behavior? Commented Jul 9, 2018 at 12:21
  • no error and no behavior, it happens nothing Commented Jul 9, 2018 at 12:22
  • if you want to remove dynamically check this stackoverflow.com/questions/44939878/… Commented Jul 9, 2018 at 12:30

1 Answer 1

2

You can remove and add dynamically using *ngIf

HTML:

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

TS:

showComponent: boolean = false
Sign up to request clarification or add additional context in comments.

3 Comments

thanks but I would prefer remove it, because it has some problems with drag and drop library
@cucuru It's not clear what you mean remove it, *ngIf does remove it from the DOM
hello @cucuru *ngIf will remove the dom from view

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.