如何从任何地方closuresAngular UI Modal

我正在使用Angular UI引导模式对话框并在服务中创build它:

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 } }; }]); 

如何从控制器调用ModalService.close()时closures所有的模态实例?

注入$modalStack服务并调用函数$modalStack.dismissAll() ,详细信息请参阅GitHub上的代码:

 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); } }; }]); 

我添加了下面的行来防止浏览器返回button路由并closurespopup窗口。 我们需要将$ modalStack注入angular度控制器。

 event.preventDefault(); $modalStack.dismissAll('close');