29

I'm using the Angular Material dialog to show a warning message in my app.

I need to check if a dialog is already open like this:

private _openDialog() {
  // if (this.dialog.isOpen()) return; <-- NOT WORKING
  this.dialog.open(WarningComponent, {
    width: '450px',
    height: '380px',
  });

}

Question: Is there any way to check if a Angular Material Dialog box is already open?

4 Answers 4

55

If it is in a single component, just store the ref. Useful for manipulating it.

private _openDialog() {
  if (!this.dialogRef) return;
  this.dialogRef = this.dialog.open(WarningComponent, {
    width: '450px',
    height: '380px',
  });

  this.dialogRef.afterClosed().pipe(
    finalize(() => this.dialogRef = undefined)
  );
}

if it's across components, check for the list of opened dialogs :

private _openDialog() {
  if (!this.dialog.openDialogs || !this.dialog.openDialogs.length) return;
  this.dialog.open(WarningComponent, {
    width: '450px',
    height: '380px',
  });
}
Sign up to request clarification or add additional context in comments.

15 Comments

Why not just use a boolean flag?
If you already know how to do it, why are you asking ... And yes you can, but using those ways give you a dialog reference, which means you can close the dialog if you want to. A boolean doesn't allow that.
Using a boolean flag adds many lines of codes. Was looking for a cleaner solution.
Because the second example gives a reference to an array of currently opened dialogs, and not the dialog currently opened by the component. This means you can't know which dialog belongs to your component, and you might close one that you wanted to keep opened. But then again, it is relative to what you do, and since I can't know what you want to do, I'd prefer leaving the two answers and let people decide what they want to implement.
Did it, simply added a sentence!
|
22

You can use the getState() method on the MatDialogRef:

const matDialogRef = this.matDialog.open(MyDialogComponent);
if(this.matDialogRef.getState() === MatDialogState.OPEN) {
    // The dialog is opened.
}

1 Comment

how would I access this from a separate component?
12

You can use this.dialog.getDialogById:

        const dialogExist = this.dialog.getDialogById('message-pop-up');

        if (!dialogExist) {
          this.dialog.open(MessagePopUpComponent, {
            id: 'message-pop-up',
            data: // some data
          });
        }

2 Comments

To add to the item above, getDialogById returns the dialogRef to the dialog. This also means you can subscribe to the closing of the dialog if the dialog is already open, thus meaning that several different items could be waiting on the result. I had a login dialog that was presented on a 401 return, but clearly with rest, sometimes several calls where made - it meant that all could retry after the log in had occured and the dialog was closed.
This solution is what I was looking for, thank you.
6

My solution was to declare a boolean

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');
});
}

1 Comment

If this is a component, you should make the isLoginDialogOpen a singleton. Otherwise if there is more than one instance you could still open it twice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.