0
我被卡住了动态组件的破坏。我会很感激一些提示。 这里我的根组件,它完美地增加了一个页面从服务的一些组件上:这是由根组件添加Angular 2.如何动态创建的组件可以自行调用destroy方法?
/*Root*/
@Component({
selector: 'convertors',
template: "<div #target></div>"})
export class AppComponent {
@ViewChild('target', {read: ViewContainerRef}) target: ViewContainerRef;
private componentRef: ComponentRef<any>;
constructor(private componentFactoryResolver: ComponentFactoryResolver,
private viewContainerRef: ViewContainerRef){}
addComponent(){
let someComponent = this.service.getService("someComponent");
const factory = this.componentFactoryResolver.resolveComponentFactory(someComponent);
this.componentRef = this.target.createComponent(factory);
}}
这里我的孩子组成。它必须自毁:
@Component({
selector: 'convertors',
template: "<button (click)="deleteComponent()" >Delete</button>"})
export class someComponent{
deleteComponent(){
/*How could I implement this function?*/
}
}
我该如何实现deleteComponent()方法? 谢谢!
好的,我找到了一个解决方案。如果有人在这个问题上需要帮助,我会回答。 –