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()方法? 謝謝!
好的,我找到了一個解決方案。如果有人在這個問題上需要幫助,我會回答。 –