Universally close Angular Material's MatDialog modals with back button

It looks like it was an Angular Zone problem. I saw this solution for similar issues in my research earlier and I tried it out. I’m not sure what I did then that didn’t work, but this worked now.

      if (this.dialog.openDialogs.length > 0) {
        // If dialogs are open, close the top one
        this.ngZone.run(() => {
          this.dialog.openDialogs[this.dialog.openDialogs.length - 1].close();
        });
      }

And suddenly I’m able to close every modal across my whole app with the back button. Woohoo!

1 Like