material icon indicating copy to clipboard operation
material copied to clipboard

How I check $mdDialog is Open?

Open GustavoCostaW opened this issue 11 years ago • 19 comments

I checked the documentation, but not found any property :(

https://material.angularjs.org/0.11.2/#/api/material.components.dialog/service/$mdDialog

$mdSidenav(componentId).isOpen(); exist and $mdDialog not?

GustavoCostaW avatar Oct 08 '15 18:10 GustavoCostaW

$mdDialog should return the promise which should be resolved in case if dialog is closed.

var myDialog = $mdDialog.show(...).then(function(result) {
  return result;
}, function() {
  return false;
});

$timeout(function() {
  if(!myDialog.$$state.status) { 
   console.log("Dialog is still active");
  }
}, 5000);

romanchak avatar Oct 09 '15 11:10 romanchak

@romanchak why $mdSidenav(componentId).isOpen(); and $mdDialog not?

GustavoCostaW avatar Oct 10 '15 17:10 GustavoCostaW

"why $mdSidenav(componentId).isOpen(); and $mdDialog not?" +1

bonatoc avatar Dec 04 '15 05:12 bonatoc

+1

AaronBuxbaum avatar Jan 01 '16 22:01 AaronBuxbaum

+1

rnybergh avatar Jan 11 '16 11:01 rnybergh

+1 I need to disable global shortcuts when any dialog is open

urrri avatar Feb 24 '16 12:02 urrri

+1

LuisP89 avatar Feb 25 '16 16:02 LuisP89

barryvdh provided a workaround in a comment (issue #1978) : if(angular.element(document).find('md-dialog').length > 0) { ... }

But a $mdDialog.isOpen() would be really nice

kevintc avatar Mar 16 '16 08:03 kevintc

Agreed that a consistent API is important: $mdDialog(componentId).isOpen() will be added.

ThomasBurleson avatar Mar 16 '16 12:03 ThomasBurleson

Is there a way to have a $mdDialog().isOpen() ? (without having to provide any componentId) I would use it to know if any mdDialog is open...

kevintc avatar Mar 16 '16 13:03 kevintc

@kevintc Well, it wouldn't make sense to have $mdDialog().isOpen(), since that could be any mdDialog instance.

EDIT: on re-reading, I see what you mean, checking if there are any dialogs open. This is a decent thought.

Personally, I'd like to have the option to both use the dialog object (myDialog.isOpen()) or access by componentId ($mdDialog(componentId).isOpen()). The former is much easier to read, but you don't necessarily have access to the dialog object at any given time, which makes the latter a necessity. Including a isAnyOpen() function or something like that would also be useful.

AaronBuxbaum avatar Mar 16 '16 16:03 AaronBuxbaum

I use it in a situation where I don't actually know if a modal is open, thus checking $mdDialog().isOpen() would work pretty well for me... .isAnyOpen() returning a boolean would be nice :)

kevintc avatar Mar 17 '16 22:03 kevintc

@GustavoCostaW is posible to have more than dialog open, I saw tree open at a time, I know that this does not follow material design guidelines.

gabrielAnzaldo avatar May 26 '16 14:05 gabrielAnzaldo

This issue is closed as part of our ‘Surge Focus on Material 2' efforts. For details, see our forum posting @ http://bit.ly/1UhZyWs.

ThomasBurleson avatar Jun 02 '16 13:06 ThomasBurleson

When any $mdDialog is opened, a css class "md-dialog-is-showing" is added to , so you could check by: angular.element(document.body).hasClass('md-dialog-is-showing')

Eliyar avatar Nov 16 '16 00:11 Eliyar

Thanks @Eliyar

DallanQ avatar Nov 17 '16 05:11 DallanQ

Hi @Eliyar, in my case I need to close any $md-dialog that I not know their componentId and your solution works fine.

Thanks so much

biruelrick avatar Oct 27 '17 22:10 biruelrick

Thanks @Eliyar

webomjaipur avatar Mar 15 '18 10:03 webomjaipur

An example of how to listen for an event when any modal opens ou closes:

var element = document.body;
var observer = new MutationObserver(function(mutationsList) {
  for (let mutation of mutationsList) {
    if (mutation.target.classList.contains("md-dialog-is-showing")) {
      console.log("some modal opened");
    } else {
      console.log("some modal closed");
    }
  }
});

observer.observe(element, {
  attributes: true,
  attributeFilter: ["class"],
  childList: false,
  characterData: false
});

// Remember to disconnect this observer after you use it.
// Maybe on route change or something
// observer.disconnect();

santospatrick avatar Feb 26 '20 13:02 santospatrick