【问题标题】:Show confirm popup on clicking outside the modal - bootstrap在模式外单击时显示确认弹出窗口 - 引导程序
【发布时间】:2017-05-11 18:10:39
【问题描述】:

我在我的应用程序和 ui-bootstrap 中使用 AngularJS (1.5.9)。 如果用户在已经打开的模式之外点击,我想显示一个确认弹出窗口。

我尝试使用$uibModalInstance.close() 的受控出口和$uibModalInstance.dismiss() 的非受控出口,但随后模式关闭,一旦我显示我的确认弹出窗口,就为时已晚。

尝试使用backdrop: 'static' 属性,但无法捕获模态窗口外的点击事件。

HTML:

<div ng-app="myApp">
    <script type="text/ng-template" id="myModal.html">
        <div class="modal-header">
            <h3 class="modal-title">Modal title</h3>
        </div>
        <div class="modal-body">
            Modal content
        </div>
        <div class="modal-footer">
            <button class="btn btn-primary" ng-click="ok()">OK</button>
            <button class="btn btn-warning" ng-click="cancel()">Cancel</button>
        </div>
    </script>

    <div ng-controller="MyCtrl">
        <input type="button" value="Show modal" ng-click="showModal()"/>
    </div>
</div>

JS:

angular.module("myApp", ['ui.bootstrap'])
.controller("MyCtrl", function($scope, $modal) {
    $scope.showModal = function(){
        $modal.open({
              templateUrl: 'myModal.html',
              controller: 'ModalDialogController', 
         })
        .result.then(
            function () {
                alert("OK");
            }, 
            function () {
                alert("Cancel");
            }
        );
    }
})

.controller("ModalDialogController", function ($scope, $modalInstance) {
  $scope.ok = function () {
    $modalInstance.close();
  };

  $scope.cancel = function () {
    $modalInstance.dismiss('Cancel');
  };
});

这是一个相关的小提琴 --> Fiddle

如您所见,一旦打开模态并单击外部,就会出现带有“取消”的警报,但我想弹出另一个模态以确认此操作以关闭模态,以防用户单击“取消”以前的模式保持打开状态。如果用户点击“确定”关闭两个模式。

有什么想法吗?

【问题讨论】:

    标签: javascript angularjs angular-ui-bootstrap


    【解决方案1】:
    Please use backdrop: 'static' after controller: 'ModalDialogController'. I am damn sure this code will be working
    
    
    angular.module("myApp", ['ui.bootstrap'])
    .controller("MyCtrl", function($scope, $modal) {
        $scope.showModal = function(){
            $modal.open({
                  templateUrl: 'myModal.html',
                  controller: 'ModalDialogController', 
                  backdrop: 'static'
    
             })
            .result.then(
                function () {
                    alert("OK");
                }, 
                function () {
                    alert("Cancel");
                }
            );
        }
    })
    
    .controller("ModalDialogController", function ($scope, $modalInstance) {
      $scope.ok = function () {
        $modalInstance.close();
      };
    
      $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
      };
    });
    

    【讨论】:

    • 感谢您的评论。如问题中所述,这并不能解决我的问题。此解决方案将阻止关闭模式,但我想弹出另一个确认用户想要关闭它的弹出窗口。
    • 我使用 backdrop: 'static' 当我想在弹出窗口之外单击并且弹出窗口不是隐藏的。我正在复制您的代码,并使用背景。它工作正常
    • 点击外部我想看到另一个弹出窗口询问“您确定要关闭吗?”如果用户单击确定,请关闭所有模式。使用背景:“静态”在模式外单击时没有任何反应
    【解决方案2】:
        angular.module("myApp", ['ui.bootstrap'])
       .controller("MyCtrl", function($scope, $modal) {
        $scope.showModal = function(){
            $modal.open({
                  templateUrl: 'myModal.html',
                  controller: 'ModalDialogController', 
             })
            .result.then(
                function () {
                    alert("OK");
                }, 
                function () {
                    var con=confirm("Are you sure?");
                    if(con)
                    alert("Confirm");
                    else
                    alert("Cancelled");
                }
            );
        }
    })
    

    【讨论】:

    • 但如果我在确认弹出窗口中点击“取消”,主模式将关闭。它应该保持打开状态。
    【解决方案3】:

    请尝试以下演示:

    曾使用“modal.closing”:此事件在模态关闭之前广播到模态范围。如果侦听器在事件上调用 preventDefault(),则模式将保持打开状态。此外,如果事件被执行,$close 和 $dismiss 方法返回 true。此事件还包括一个结果/原因参数和一个布尔值,指示模式是关闭 (true) 还是关闭。

    您可以将 javascript 确认替换为确认模式弹出窗口。

    angular.module('app', ['ui.bootstrap'])
    
    .controller('modalCtrl', function ($scope, $uibModalInstance) {
    
        $scope.$on('modal.closing', function (event, reason, closed) {
            if (!closed) {
    
                if (!confirm('Are you sure you wanna close the modal? the database?')) {
                    event.preventDefault();
                }
            }
        });
    
        $scope.ok = function () {
            $uibModalInstance.close();
        };
    
        $scope.cancel = function () {
            $uibModalInstance.dismiss('cancel');
        };
    })
    
    
    .controller('appCtrl', function ($scope, $uibModal) {
    
        $scope.open = function () {
            var modalInstance = $uibModal.open({
                templateUrl: 'myModal.html',
                controller: 'modalCtrl'
            }).result.then(
                    function () {
                        alert("OK");
                    },
                    function () {
                        alert("Cancel");
                    }
                );
    
        }
    });
    <!DOCTYPE html>
    <html>
    
      <head>
        
        <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.js"></script>
       <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/2.3.2/ui-bootstrap-tpls.min.js"></script>
        
        <link data-require="bootstrap-css@*" data-semver="3.3.1" rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" />
      </head>
    
      <body ng-app="app" ng-controller="appCtrl">
        <button ng-click="open()">Click me!</button>
         <script type="text/ng-template" id="myModal.html">
            <div class="modal-header">
                <h3 class="modal-title">Modal title</h3>
            </div>
            <div class="modal-body">
                Modal content
            </div>
            <div class="modal-footer">
                <button class="btn btn-primary" ng-click="ok()">OK</button>
                <button class="btn btn-warning" ng-click="cancel()">Cancel</button>
            </div>
        </script>
      </body>
    
    </html>

    【讨论】:

    • 太棒了!谢谢!不知道我怎么错过了这个 modal.closing 功能。
    猜你喜欢
    • 2015-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-27
    • 1970-01-01
    • 1970-01-01
    • 2010-09-22
    相关资源
    最近更新 更多