我有一个可以动态地将组件推送到 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;
有什么想法吗..?
最佳答案
您所需要的只是重新初始化this.currentComponent
:
this.currentComponent = [];
在 for(let t of data)
<强> WORKING DEMO
关于javascript - 从 DOM 中删除组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48759113/