Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to automatically close Angular UI Bootstrap modal when route changes?

I've got links in templates inside modals. When I click them, the current page changes, but the overlay and modal stay. I could add ng-click="dimiss()" to every link in all templates in modals, but is there a better way? E.g. to close it automatically on successful route change or add just one ng-click per template to handle all links?

like image 565
szimek Avatar asked May 20 '14 14:05

szimek


2 Answers

If you want all the opened modals to be closed whenever a route is changed successfully, you could do it in one central place by listening to the $routeChangeSuccess event, for example in a run block of your app:

var myApp = angular.module('app', []).run(function($rootScope, $uibModalStack) {   $uibModalStack.dismissAll(); });  

Here you can see that the $uibModalStack service gets injected on which you can call the dismissAll method - this call will close all the currently opened modals.

So, yes, you can handle modals closing centrally, in one place, with one line of code :-)

like image 129
pkozlowski.opensource Avatar answered Sep 21 '22 01:09

pkozlowski.opensource


A better way is to see that whenever a Popup (Modal) is open, on Browser Back button click (or Keyboard Back), we stop the URL change and just close the Popup. This works for a better User Experience in my Project.

The Browser Back button works normally if there is no Modal opened.

use:

$uibModalStack.dismiss(openedModal.key); 

or

$uibModalStack.dismissAll; 

Sample code:

.run(['$rootScope', '$uibModalStack',     function ($rootScope,  $uibModalStack) {                // close the opened modal on location change.         $rootScope.$on('$locationChangeStart', function ($event) {             var openedModal = $uibModalStack.getTop();             if (openedModal) {                 if (!!$event.preventDefault) {                     $event.preventDefault();                 }                 if (!!$event.stopPropagation) {                     $event.stopPropagation();                 }                 $uibModalStack.dismiss(openedModal.key);             }         });     }]); 
like image 25
GamaSharma Avatar answered Sep 19 '22 01:09

GamaSharma