Angular如何使用其对象引用删除组件实例

时间:2018-12-02 19:24:12

标签: angular dynamic components

CLI 6.2.1 Angular 6.1.7-我根据用户请求动态创建组件。 HTML将每个实例显示为已构建,并且它们是正确且唯一的。

在某些时候,我需要删除其中一个(或多个)。我的意思是,我不仅不需要DOM表示,而且根本不需要那个特定的实例或对象引用。

新信息:这是组件的创建方式:

    createComponent(componentType): any {

    const factory: ComponentFactory<any> = this.resolver.resolveComponentFactory(componentType);
    this.componentRef = this.container.createComponent(factory);
    return this.componentRef;  // return the REFERENCE containing the instance.
  }

我尝试过:

Items.objRef.delete;
Items.objRef.delete();
Items.objRef.instance.delete;
Items.objRef.instance.delete();

在此之后,我期望:

Items.objRef = null;  // just to clean up

,然后根据需要创建一个新的

我需要强制Angular进行“ detectChanges()”吗?

还有其他想法吗? 提前致谢, 查克又名瑜伽士。

0 个答案:

没有答案