【问题标题】:How to remove component from view?如何从视图中删除组件?
【发布时间】:2018-12-17 02:53:23
【问题描述】:

我想从我的父组件中动态删除组件。

<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>

子组件发射到父组件,它在 onRemove 函数中起作用:

constructor(private generalViewContainerRef: ViewContainerRef) {  
}

private onRemoveWidget(index:number) {

     this.generalViewContainerRef.remove(component);
}

但它不起作用。

我该怎么做?

【问题讨论】:

标签: angular angular5


【解决方案1】:

您可以使用*ngIf动态删除和添加

HTML:

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

TS:

showComponent: boolean = false

【讨论】:

  • 谢谢,但我宁愿删除它,因为它在拖放库方面存在一些问题
  • @cucuru 不清楚你的意思删除它*ngIf 确实将它从 DOM 中删除
  • hello @cucuru *ngIf 将从视图中删除 dom
猜你喜欢
  • 2020-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-21
  • 2012-05-04
相关资源
最近更新 更多