Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to close Angular UI Modal from anywhere

I am using the Angular UI bootstrap modal dialog and create it within a service:

myApp.factory('ModalService', ['$modal', function($modal) {     return {         trigger: function(template) {             $modal.open({                 templateUrl: template,                 size: 'lg',                 controller: function($scope, $modalInstance) {                     $scope.ok = function() {                         $modalInstance.close($scope.selected.item);                     };                     $scope.cancel = function() {                         $modalInstance.dismiss('cancel');                     };                 }             });         },         close: function() {             // this should close all modal instances         }     }; }]); 

How can I close all modal instances when calling ModalService.close() from a controller or whatsoever?

like image 966
DonJuwe Avatar asked Aug 22 '14 11:08

DonJuwe


People also ask

How to close the modal in Angular?

Opening & Closing Angular 8 Modal Dialogs open('custom-modal-1') . To close a modal call the modalService. close() method with the id of the modal you want to close, e.g. modalService. close('custom-modal-1') .

What is modal in Angular 8?

$uibModal is a service to create modal windows. It has an open method, that will return a modal instance. Show activity on this post. It is a service for opening modals in AngularJs.


1 Answers

Inject the $modalStack service and call the function $modalStack.dismissAll(), see the code on GitHub for details:

myApp.factory('ModalService', ['$modal', '$modalStack' function($modal, $modalStack) {     return {         trigger: function(template) {             $modal.open({                 templateUrl: template,                 size: 'lg',                 controller: function($scope, $modalInstance) {                     $scope.ok = function() {                         $modalInstance.close($scope.selected.item);                     };                     $scope.cancel = function() {                         $modalInstance.dismiss('cancel');                     };                 }             });         },         close: function(reason) {             $modalStack.dismissAll(reason);         }     }; }]); 
like image 162
apairet Avatar answered Nov 08 '22 23:11

apairet