【发布时间】:2019-11-11 09:39:56
【问题描述】:
我正在使用它来动态创建 angular 组件:
addComponent() {
const componentFactory = this.componentFactoryResolver.resolveComponentFactory(ChildComponent);
const viewContainerRef = this.injectComp.viewContainerRef;
const compRef = viewContainerRef.createComponent(componentFactory);
compRef.instance.someProperty = "some data";
}
因此,每次执行该方法时,都会创建一个新的组件实例。 到那里为止,一切都很好,但我的问题是:
如何通过按钮单击事件从 ChildComponent 本身销毁这些创建的组件?
【问题讨论】:
-
自关闭的意思——你想销毁组件?
-
是的,如果我创建 5 个组件(例如)...我希望能够单击组件上的按钮并自行销毁它
-
这不是我想要的。我想在 childComponent 本身上触发自毁。能做到吗?