2

My application can open a modal if there's already one open. If that's the case I want to close that modal and open the new one after that's done.

Service to open modals:

app.service('ModalService', function($uibModal) {
this.open = function(size, template, content, backdrop, controller) {
    var modalInstance = $uibModal.open({
        animation: true,
        templateUrl: content,
        windowTemplateUrl: template,
        controller: controller,
        backdrop: backdrop,
        size: size,
        resolve: {}
    });
    return modalInstance;
};

Then I open one with:

var m = ModalService.open('lg', '', 'ng-templates/modal.html', true, 'ModalController');

And I can close it with:

m.close();

I can only use m.close() within the same switch/case as I open the modal. If I want to close it in another if statement later in the code m is undefined.

Anyway. Can I check if a modal is open? If I console.log(m) then there's this:

d.$$state.status = 1
d.$$state.value = true

But I cannot access the variable m elsewhere in my app so I cannot check for that.

3

Just add an flag or getter to your ModalService.

app.service('ModalService', function($uibModal) {
var open = false,
    modalInstance;

this.isOpen = function () {
  return open;
};

this.close = function (result) {
  modalInstance.close(result);
};

this.dismiss = function (reason) {
  modalInstance.dismiss(reason);
};

this.open = function(size, template, content, backdrop, controller) {
    var modal = $uibModal.open({
        animation: true,
        templateUrl: content,
        windowTemplateUrl: template,
        controller: controller,
        backdrop: backdrop,
        size: size,
        resolve: {}
    });

    //Set open
    open = true;

    //Set modalInstance
    modalInstance = modal;

    //Modal is closed/resolved/dismissed
    modal.result.finally(function () {
      open = false;
    });

    return modal;
};
}

You can then call: ModalService.isOpen() to check if your modal is opened.

|improve this answer|||||
  • Pretty neat, will give it a shot and report back. Thanks. – Bento Sep 30 '16 at 12:56
  • This works. Any idea how I can close the modal? I've now replaced var m with $rootScope.m which allows me to access m everywhere in my app. Wondering if there's a better solution. $rootScope.m = ModalService.open('lg', '', 'ng-templates/modal.html', true, 'ModalController'); Then I do: $rootScope.m.close(); – Bento Sep 30 '16 at 12:59
  • Just store the modalInstance like open as a local variable and implement an additional function (for example: ModalService.closeModal()) and close the modal inside your service. The question is why you want to close the modal from outside its scope. – Code Spirit Sep 30 '16 at 13:02
  • I might need to close it from another controller or in another if statement. Using a var doesn't work but $rootScope.variable does. – Bento Sep 30 '16 at 13:09
  • I've made an edit and added ModalService.dismiss(reason) and ModalService.close(result) methods. I left the checking if modalInstance is set, you should add it before using it in your code. – Code Spirit Sep 30 '16 at 14:21
2

Very easy:

Since $uibModal always uses a div with class name modal you can just check if any element with that class name exist:

//If no modal is open
if (document.getElementsByClassName("modal").length === 0) {
  //do something...
} else {
  // do something when a modal is open
}
|improve this answer|||||
  • 1
    hmm thats dirty check, not really the best soln, especially if you have multiple modal opens at the same time. – roger Aug 8 '18 at 4:38
0

Since the $uibModal service doesn't provide it, the leanest way is to check for "modal-open" class on the body:

document.body.className.indexOf('modal-open') !== -1
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.