Vin*_*oft 4 typescript angular-material angular
我正在使用“ 角度材质”对话框在我的应用中显示警告消息。
我需要检查对话框是否已经像这样打开:
private _openDialog() {
// if (this.dialog.isOpen()) return; <-- NOT WORKING
this.dialog.open(WarningComponent, {
width: '450px',
height: '380px',
});
Run Code Online (Sandbox Code Playgroud)
}
问题:有什么方法可以检查角度材料对话框是否已打开?
Jon*_*gon 21
getState()您可以在以下位置使用该方法MatDialogRef:
const matDialogRef = this.matDialog.open(MyDialogComponent);
if(this.matDialogRef.getState() === MatDialogState.OPEN) {
// The dialog is opened.
}
Run Code Online (Sandbox Code Playgroud)
小智 10
如果在单个组件中,则只需存储引用。对操纵它很有用。
private _openDialog() {
if (!this.dialogRef) return;
this.dialogRef = this.dialog.open(WarningComponent, {
width: '450px',
height: '380px',
});
this.dialogRef.afterClosed().pipe(
finalize(() => this.dialogRef = undefined)
);
}
Run Code Online (Sandbox Code Playgroud)
如果跨组件,请检查打开的对话框列表:
private _openDialog() {
if (!this.dialog.openDialogs || !this.dialog.openDialogs.length) return;
this.dialog.open(WarningComponent, {
width: '450px',
height: '380px',
});
}
Run Code Online (Sandbox Code Playgroud)
我的解决方案是声明一个布尔值
public isLoginDialogOpen: boolean = false; // I know by default it's false
public openLoginDialog() {
if (this.isLoginDialogOpen) {
return;
}
this.isLoginDialogOpen = true;
this.loginDialogRef = this.dialog.open(LoginDialogComponent, {
data: null,
panelClass: 'theme-dialog',
autoFocus: false
});
this.loginDialogRef.afterClosed().subscribe(result => {
this.isLoginDialogOpen = false;
console.log('The dialog was closed');
});
}
Run Code Online (Sandbox Code Playgroud)
小智 5
您可以使用 this.dialog.getDialogById:
const dialogExist = this.dialog.getDialogById('message-pop-up');
if (!dialogExist) {
this.dialog.open(MessagePopUpComponent, {
id: 'message-pop-up',
data: // some data
});
}
Run Code Online (Sandbox Code Playgroud)