从DOM中删除组件

时间:2018-02-13 03:48:41

标签: javascript angular dom

我有一个组件可以动态地将组件推送到DOM。 但是,每当我推送新的组件时,我都需要从DOM中删除旧组件。 Angular 2上的所有内容::)

如果我删除一个组件但是如果我尝试删除组件数组它将无效。插入工作正常但删除不是。 找到下面的代码段,

let resolvedInputs = ReflectiveInjector.resolve(inputProviders);

    let injector = ReflectiveInjector.fromResolvedProviders(resolvedInputs, this.dynamicComponentContainer.parentInjector);

    for(let t of data){


        //create a factory out of the component we want to create
        let factory = this.resolver.resolveComponentFactory(t.component);


        //create the component using the factory and the injector
        var component = factory.create(injector);

        this.currentComponent.push(component);
        // Insert the component into the dom container
        this.dynamicComponentContainer.insert(component.hostView);



    }

    if (this.previousComponent) {

            for(let comp of this.previousComponent){
                comp.destroy();
            }
        }

    this.previousComponent = this.currentComponent;

任何想法......?

1 个答案:

答案 0 :(得分:2)

您需要的只是重新初始化this.currentComponent

this.currentComponent = [];

之前

for(let t of data)

<强> WORKING DEMO

相关问题