Is there a way to catch the BootstapUI $ uibModal event?

I am using modal compoenent from the Bootstrap user interface ( https://angular-ui.imtqy.com/bootstrap/ ) in Angular to make modal, and when I close, I want to be able to redirect to another state, or at least call a function .

The problem is that I can catch the close event, but whenever the user presses Esc , the modal is rejected, and I could not find the documentation on how to catch the dismiss event or assign the function to the promise that returned.

The code is as follows:

 var modalInstance = $uibModal.open({ templateUrl: 'a-template.html', controller: 'AnotherCtrl', controllerAs: 'modal', backdrop: false, size: 'lg' }); // I am able to catch this whenever the user exits the modal in an // orthodox fashion. Doesn't happen when he presses Esc button. modalInstance.closed = function () { $state.go(homeState); }; 

Alternatively, another solution that suits my business case would simply not allow the user to reject the modal. And close it yourself when an event occurs in the modal controller. I have not yet found functionality for this.

+6
source share
1 answer

In the controller associated with the modal ("AnotherCtrl" or "modal" for you), you can use $scope.$on() with the 'modal.closing' event.

Example:

 var modalInstance = $uibModal.open({ templateUrl: 'a-template.html', controller: ['$scope', function($scope){ $scope.$on('modal.closing', function(event, reason, closed){ if('condition for not closing') event.preventDefault(); //You can use this to prevent the modal from closing else window.location = "https://www.youtube.com/watch?v=dQw4w9WgXcQ"; }); }], controllerAs: 'modal', backdrop: false, size: 'lg' }); 
+17
source

All Articles