javascript - 单击模式外部时显示确认弹出窗口 - bootstrap

标签 javascript angularjs angular-ui-bootstrap

我在我的应用程序和 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 --> Fiddle

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

有什么想法吗?

最佳答案

请尝试以下演示:

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

您可以用确认模式弹出窗口替换 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>

关于javascript - 单击模式外部时显示确认弹出窗口 - bootstrap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41345457/

相关文章:

javascript - 在 for 循环中发出,仅获取最后一项

javascript - 服务器数据请求后无法更新旋钮的颜色

css - 通过单击上一组中的按钮更改 UI Bootstrap Accordion 组内容

javascript - 如何通过 ui-select 值设置 ng-model 值

javascript - Knockout.js - 注册以更改 observableArray

javascript - 为按钮添加类

javascript - 如何仅获取带有值的 html 表格行列?

android - 在 cordova android 中检查 GPS 状态(开/关)

javascript - 在 node.js 中连接到大型机 api 的适配器/方法

angularjs - Angular UI Carousel 的 `track by $index` 问题