简体   繁体   English

如何从DOM中删除组件?

[英]How to remove component from DOM?

I am wondering how I can remove my component form DOM manually? 我想知道如何手动删除组件表单DOM? 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? 我一直在查看ElementRef ,并在那里找到了一些解决方法,但是我一直在想是否有专门用于执行此操作的功能?

My set up is the following: 我的设置如下:

I've got a Loader component, that is called via EventEmitter. 我有一个通过EventEmitter调用的Loader组件。 I am adding it to a view by DynamicComponentLoader.loadNextToLocation(..) , and then I want to remove it from the view. 我通过DynamicComponentLoader.loadNextToLocation(..)将其添加到视图中,然后从视图中将其删除。

I've also found Renderer.destroyView(component) , but this yields me to errors. 我还找到了Renderer.destroyView(component) ,但这使我陷入错误。

Thanks 谢谢

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 另请参阅带有用户单击所选组件的Angular 2动态选项卡

你可以使用*ngIfangular2将添加的元素到DOM如果条件结果为真,在结果假但这并没有让上呈现的DOM元素的情况下,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM