title: How to check if a Bootstrap UI modal is open? - AngularJS
body: <p>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.</p>
  
  <p>Service to open modals:</p>
  
  <pre><code>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;
  };
  </code></pre>
  
  <p>Then I open one with:</p>
  
  <pre><code>var m = ModalService.open('lg', '', 'ng-templates/modal.html', true, 'ModalController');
  </code></pre>
  
  <p>And I can close it with:</p>
  
  <pre><code>m.close();
  </code></pre>
  
  <p>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.</p>
  
  <p>Anyway. Can I check if a modal is open? If I console.log(m) then there's this:</p>
  
  <pre><code>d.$$state.status = 1
  d.$$state.value = true
  </code></pre>
  
  <p>But I cannot access the variable m elsewhere in my app so I cannot check for that.</p>