Angular -ui: is any modal open?

I want to run the code if any modal opens. Usually I want something like:

$scope.$watch(function () { return $modal.isOpenState; }, function (val) { //my code here }, true ); 

But I did not know what to see. Yes, I can detect the open event for each instance, for example:

 modalInstance.opened.then(function() { //my code here }); 

But this is not DRY.

PS I can also do something like $('.modal').hasClass('in') in $watch , but it's a little ugly

PPS And btw I use ui-router to open modals (see faq here )

  $modal.open({ templateUrl: "...", resolve: {... }, controller: function($scope) { ... } }).result.finally(function() { //can put code here, but same issue }); 
+7
javascript angularjs angularjs-scope angular-ui
source share
1 answer

There is an internal use of the service user interface called $modalStack . This service is used by $modal and has a method called getTop to retrieve the current open modal instance. So you can enter this service and just see the getTop result on $rootScope . For example:

 app.run(function($rootScope, $modalStack) { $rootScope.$watch($modalStack.getTop, function(newVal, oldVal) { if (newVal) { console.log('opened', newVal, oldVal); } }); }); 

Demo: http://plnkr.co/edit/ZVD0cryL0qXNGl9UPMxB?p=preview

+5
source share

All Articles