【问题标题】:Close ui-bootstrap modal with mouse click with no backdrop在没有背景的情况下单击鼠标关闭 ui-bootstrap 模态
【发布时间】:2016-01-14 15:09:38
【问题描述】:

我目前正在将 ui-bootstrap 从 0.5.0 迁移到 0.14.3(是的,我已经很久没有检查更新了)。在 0.5.0 中有一个带有 'options' 配置对象的 'modal' 指令。有一个“backdropClick”选项,当鼠标在它外面点击时,它允许关闭模式。在 0.14.3 中,$uibModal 服务没有这样的选项。它有一个“背景”属性,可以设置为“静态”,允许您通过鼠标单击关闭模式,但模式有背景。如果没有呢?我可以通过将“背景”属性设置为“假”的鼠标单击来关闭它吗?

【问题讨论】:

    标签: angular-ui-bootstrap


    【解决方案1】:

    这个缺失的功能有一个解决方法。 您可以使用指令检测“点击外部”事件,然后在相关事件中调用关闭模式方法。

    在下面的示例中(改编自 ui.bootstrap 文档)我使用了angular-click-outside directive

    angular.module('ui.bootstrap.demo', ['ngAnimate', 'ui.bootstrap','angular-click-outside']);
    angular.module('ui.bootstrap.demo').controller('ModalDemoCtrl', function ($scope, $uibModal, $log, $document) {
    
      $scope.items = ['item1', 'item2', 'item3'];
    
      $scope.animationsEnabled = true;
      
      $document.on('click', function(event) {
        
      });
    
      $scope.open = function (size) {
    
        var modalInstance = $uibModal.open({
          animation: $scope.animationsEnabled,
          templateUrl: 'myModalContent.html',
          controller: 'ModalInstanceCtrl',
          backdrop: false,
          size: size,
          resolve: {
            items: function () {
              return $scope.items;
            }
          }
        });
    
        modalInstance.result.then(function (selectedItem) {
          $scope.selected = selectedItem;
        }, function () {
          $log.info('Modal dismissed at: ' + new Date());
        });
      };
    
      $scope.toggleAnimation = function () {
        $scope.animationsEnabled = !$scope.animationsEnabled;
      };
    
    });
    
    // Please note that $modalInstance represents a modal window (instance) dependency.
    // It is not the same as the $uibModal service used above.
    
    angular.module('ui.bootstrap.demo').controller('ModalInstanceCtrl', function ($scope, $uibModalInstance, items) {
    
      $scope.items = items;
      $scope.selected = {
        item: $scope.items[0]
      };
    
      $scope.ok = function () {
        $uibModalInstance.close($scope.selected.item);
      };
    
      $scope.cancel = function () {
        $uibModalInstance.dismiss('cancel');
      };
    });
    <!doctype html>
    <html ng-app="ui.bootstrap.demo">
      <head>
        <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.js"></script>
        <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-animate.js"></script>
        <script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-1.0.3.js"></script>
        <script src="example.js"></script>
        <link href="//netdna.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
        <script src="//rawgit.com/IamAdamJowett/angular-click-outside/master/clickoutside.directive.js"></script>
        
      </head>
      <body>
    
    <div ng-controller="ModalDemoCtrl">
        <script type="text/ng-template" id="myModalContent.html">
          <div id="my-modal" click-outside="cancel()">
            <div class="modal-header">
                <h3 class="modal-title">I'm a modal!</h3>
            </div>
            <div class="modal-body">
                <ul>
                    <li ng-repeat="item in items">
                        <a href="#" ng-click="$event.preventDefault(); selected.item = item">{{ item }}</a>
                    </li>
                </ul>
                Selected: <b>{{ selected.item }}</b>
            </div>
            <div class="modal-footer">
                <button class="btn btn-primary" type="button" ng-click="ok()">OK</button>
                <button class="btn btn-warning" type="button" ng-click="cancel()">Cancel</button>
            </div>
          </div>
        </script>
    
        <button type="button" class="btn btn-default" ng-click="open()">Open me!</button>
        <button type="button" class="btn btn-default" ng-click="open('lg')">Large modal</button>
        <button type="button" class="btn btn-default" ng-click="open('sm')">Small modal</button>
        <button type="button" class="btn btn-default" ng-click="toggleAnimation()">Toggle Animation ({{ animationsEnabled }})</button>
        <div ng-show="selected">Selection from a modal: {{ selected }}</div>
    </div>
      </body>
    </html>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-05
      • 2014-03-29
      • 2019-10-28
      • 2014-02-17
      • 2021-02-12
      • 1970-01-01
      相关资源
      最近更新 更多