How I check $mdDialog is Open?
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?
$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 why $mdSidenav(componentId).isOpen(); and $mdDialog not?
"why $mdSidenav(componentId).isOpen(); and $mdDialog not?" +1
+1
+1
+1 I need to disable global shortcuts when any dialog is open
+1
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
Agreed that a consistent API is important: $mdDialog(componentId).isOpen() will be added.
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 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.
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 :)
@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.
This issue is closed as part of our ‘Surge Focus on Material 2' efforts. For details, see our forum posting @ http://bit.ly/1UhZyWs.
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')
Thanks @Eliyar
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
Thanks @Eliyar
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();