如何从任何地方关闭Angular UI Modal

Don*_*uwe 39 angularjs angular-ui angularjs-service angular-ui-bootstrap

我正在使用Angular UI bootstrap模式对话框并在服务中创建它:

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
        }
    };
}]);
Run Code Online (Sandbox Code Playgroud)

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

apa*_*ret 87

注入$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);
        }
    };
}]);
Run Code Online (Sandbox Code Playgroud)

  • 真棒.它也适用于AngularUI,只需使用`$ uibModalStack`. (18认同)
  • 谢谢,像魅力一样!我不知道为什么文档中没有提到这个问题https://github.com/angular-ui/bootstrap/tree/master/src/modal/docs (5认同)
  • @DonJuwe同意,在关闭$ http成功的模式时会很有帮助. (2认同)