Flygenring Flygenring - 1 year ago 152
AngularJS Question

Using Angular Material, is it possible to close a specific dialog

I have an AngularJS app using the Angular Material UI framework.

The app has different mechanisms showing dialogs (e.g error and loading spinner) and it would be preferable to only close one specifically chosen in certain scenarios, e.g. when an AJAX request is finished fetching data, I would like my loading spinner to close, but not any error dialog that may be the result of the fetching.

What I can find in documentation and code doesn't agree (though code should win the argument):

  • Documentation says only the latest can be closed, with an optional response

  • The code says the latest, a number of latest or all open can be closed, with an optional reason

  • Example in the documentation says a specific dialog can be closed, with a flag denoting how or why

I have made a demo of my intent, as MCV as possible – these are the highlights:

var dialog = {},
promise = {};

function showDialogs(sourceEvent) {
showDialog(sourceEvent, "one");
showDialog(sourceEvent, "two");

function showDialog(sourceEvent, id) {
dialog[id] = $mdDialog.alert({...});

promise[id] = $[id]);
promise[id].finally(function() {
dialog[id] = undefined;

function closeDialogs() {
$mdDialog.hide("Closed all for a reason", {closeAll: true});

function closeDialogLatest() {
$mdDialog.hide("Closed from the outside");

function closeDialogReason() {
$mdDialog.hide("Closed with a reason");

function closeDialogSpecific(id) {
$mdDialog.hide(dialog[id], "finished");


I know the code always wins the argument about what happens, but I wasn't entirely sure it was the right code I was looking at.

I have updated the examples to better test and illustrate my point and problem. This shows things to work as the code said.

What I'm really looking for is whether it might still be possible to achieve my goal in some other way that I didn't think of yet.

Answer Source

Using $mdPanel instead of $mdDialog I was able to achieve the desired effect; I forked my demo to reflect the changes – these are the highlights:

var dialog = {};

function showDialogs() {

function showDialog(id) {
    var config = {...};

        .then(function(panelRef) {
            dialog[id] = panelRef;

function closeDialogs() {
    var id;

    for(id in dialog) {
        closeDialogSpecific(id, "Closed all for a reason");

function closeDialogSpecific(id, reason) {
    var message = reason || "finished: " + id;

    if(!dialog.hasOwnProperty(id) || !angular.isObject(dialog[id])) {

    if(dialog[id] && dialog[id].close) {
            .then(function() {
       = message;
        dialog[id] = undefined;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download