是否有适当的方法来销毁由对话框创建的组件实例。 当我关闭对话框时,组件实例未被处理:
import { Component, OnInit, Input, Output, EventEmitter, Inject } from '@angular/core';
import { MdDialog, MdDialogRef, MD_DIALOG_DATA } from '@angular/material';
@Component({
selector: 'basket',
templateUrl: './basket.component.html',
styleUrls: ['./basket.component.css']
})
export class BasketComponent implements OnInit {
@Input() Product: any;
}
@Component({
selector: 'basket-dialog',
template: '<div><basket [Product]="Product"></basket></div>'
})
export class BasketDialogComponent implements OnInit {
Product: any;
constructor(public dialogRef: MdDialogRef<BasketComponent>,
@Inject(MD_DIALOG_DATA) public productData: any) { }
ngOnInit() {
this.Product = this.productData;
this.say();
}
say() {
setTimeout(() => this.say(), 1000);
console.log('test');
}
}
创建对话框:
let ordersDialog = this.dialog.open(BasketDialogComponent, {
data: product
});
ordersDialog.afterClosed().subscribe(x => {
// something like: orderDialog.componentInstance.dispose();
});
即使对话框关闭,say()
方法仍在执行。
最佳答案
你应该关心自己处理setTimeout
:
export class BasketDialogComponent implements OnInit, OnDestroy {
timeoutId;
say() {
this.timeoutId = setTimeout(() => this.say(), 1000);
console.log('test');
}
ngOnDestroy() {
if (this.timeoutId) {
clearTimeout(this.timeoutId);
}
}
}
关于Angular Material 销毁对话框实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46254297/