1

I am wondering how I can remove my component form DOM manually? I've been looking at ElementRef, and found some work arounds there, but I've been wondering if there is any function dedicated to do this?

My set up is the following:

I've got a Loader component, that is called via EventEmitter. I am adding it to a view by DynamicComponentLoader.loadNextToLocation(..), and then I want to remove it from the view.

I've also found Renderer.destroyView(component), but this yields me to errors.

Thanks

uksz
  • 18,239
  • 30
  • 94
  • 161

2 Answers2

3
this.dcl.loadIntoLocation(this.type, this.elRef, 'target').then((cmpRef) => {
  this.cmpRef = cmpRef;
});

then remove it with:

this.cmpRef.dispose();

See also Angular 2 dynamic tabs with user-click chosen components

Community
  • 1
  • 1
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
-1

you can use *ngIfof angular2 which will added the Element to DOM if condition result is true, In case of results False it does't allow to render the Element on DOM

Pardeep Jain
  • 84,110
  • 37
  • 165
  • 215
akhil viswam
  • 496
  • 9
  • 24
  • I don't think this is a valid solution because I think meanwhile the component is checking for the *ngIf condition to be true it is still loaded into memory and using resources. I guess this question is about how to destroy the component, not just to make it not visible. – Osakr Nov 08 '18 at 10:37