【问题标题】:Angular Bootstrap Modal leaves backdrop openAngular Bootstrap Modal 打开背景
【发布时间】:2015-05-12 12:48:15
【问题描述】:

我正在使用 AngularUI 将 Bootstrap 组件集成到我的 Angular 1.4 应用程序中,例如 Modals。

我在我的控制器中调用 Modal,如下所示:

var modalInstance = $modal.open({
  animation: true,
  templateUrl: '/static/templates/support-report-modal.html',
  controller: 'ModalInstanceCtrl'
});

不幸的是,当我想通过以下方式关闭 Modal 时:

modalInstance.close();

模态框本身消失了,背景也消失了,但它并没有从 DOM 中移除,所以它覆盖了整个页面,使页面没有响应。

当我检查时,我看到了这个:

https://angular-ui.github.io/bootstrap/#/modal 文档中的示例中,modal-open 类已从正文中删除,整个 modal-backdrop 在关闭时从 DOM 中删除。 在我的示例中,为什么 Modal 淡出但背景没有从 DOM 中移除?

我已经检查了许多关于引导模态背景的其他问题,但我似乎无法弄清楚出了什么问题。

【问题讨论】:

  • 您是否在控制台中遇到任何错误?
  • 无...随着模态和背景淡出,似乎一切正常。但是背景就在那里....
  • 这显然是由于一个错误。 AngularUI 还不支持 Angular 1.4。为后代记录:github.com/angular-ui/bootstrap/issues/3620
  • 使用 Angular UI 0.13.1 和 Angular 1.4.x 已经解决了这个问题。 Angular UI 0.13.0 出现了这个错误。

标签: angularjs angular-ui angular-ui-bootstrap bootstrap-modal


【解决方案1】:

这显然是由于一个错误。 AngularUI 还不支持 Angular 1.4。一旦http://github.com/angular-ui/bootstrap/issues/3620 得到解决,这将起作用。

【讨论】:

  • 非常感谢...问题还没有解决,但是通过禁用动画,背景会被移除,所以它可以解决,至少现在是这样。
  • 使用 Angular UI 0.13.1 和 Angular 1.4.x 已经解决了这个问题。 Angular UI 0.13.0 出现了这个错误。
【解决方案2】:

在团队在这里解决这个问题之前,这是一个变通办法。

<div class="modal-footer">
    <button class="btn btn-primary"
        ng-click="registerModal.ok()"
        remove-modal>OK</button>
    <button class="btn btn-warning"
        ng-click="registerModal.cancel()"
        remove-modal>Cancel</button>
</div>

/*global angular */
(function () {
'use strict';
angular.module('CorvetteClub.removemodal.directive', [])
.directive('removeModal', ['$document', function ($document) {
    return {
        restrict: 'A',
        link: function (scope, element, attrs) {
            element.bind('click', function () {
                $document[0].body.classList.remove('modal-open');
                    angular.element($document[0].getElementsByClassName('modal-backdrop')).remove();
                    angular.element($document[0].getElementsByClassName('modal')).remove();
                });
            }
        };
    }]);
}());

不幸的是,团队似乎在这个问题上不在同一个页面上,因为它被贡献者推送到一个单独的线程,然后它被推送到的线程被另一个人关闭,因为它被认为是“离题”另一个。

【讨论】:

  • 我已经确认了在 github 问题 3633 底部发现的陈述的真实性:“这似乎现在适用于 angular 1.4.3 和 angular-bootstrap 0.13.1。”
【解决方案3】:

你可以这样做,先关闭你打开的模态

 $('#nameOfModal').modal('hide');  

基本上是模态的 id 第二个,如果有的话要删除

 $('body').removeClass('modal-open');

最后关闭背景

 $('.modal-backdrop').remove();

【讨论】:

    【解决方案4】:
    <button type="button" class="close" onclick="$('.modal-backdrop').remove();"
                                    data-dismiss="modal">
    
    $(document).keypress(function(e) { 
        if (e.keyCode == 27) { 
           $('.modal-backdrop').remove();
        } 
    });
    

    【讨论】:

    • 感谢您提供此代码 sn-p,它可能会提供一些有限的短期帮助。一个正确的解释would greatly improve 其长期价值,通过展示为什么这是解决问题的好方法,并将使其对未来有其他类似问题的读者更有用。请edit您的回答添加一些解释,包括您所做的假设。
    【解决方案5】:

    我使用的是 Angular 1.3.13 版并且有类似的问题。我一直在研究这个问题,并相信这个错误从 Angular 版本 1.3.13 延伸到 1.4.1 细节https://github.com/angular-ui/bootstrap/pull/3400

    如果您滚动到该链接的底部,您将看到 fernandojunior 的帖子,其中显示了他测试和升级到的版本仍然显示相同的问题。他甚至创建了一个 plnker 来模拟问题 http://plnkr.co/edit/xQOL58HDXTuvSDsHRbra,我已经使用 Angular-UI 模态代码示例在下面的代码 sn-p 中模拟了问题。

    // angular.module('ui.bootstrap.demo', ['ngAnimate', 'ui.bootstrap']);
    
    angular
      .module('ui.bootstrap.demo', [
        'ngAnimate',
        'ui.bootstrap',
      ]);
    
    angular.module('ui.bootstrap.demo').controller('ModalDemoCtrl', function ($scope, $modal, $log) {
    
      $scope.items = ['item1', 'item2', 'item3'];
    
      $scope.animationsEnabled = true;
    
      $scope.open = function (size) {
    
        var modalInstance = $modal.open({
          animation: $scope.animationsEnabled,
          templateUrl: 'myModalContent.html',
          controller: 'ModalInstanceCtrl',
          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 $modal service used above.
    
    angular.module('ui.bootstrap.demo').controller('ModalInstanceCtrl', function ($scope, $modalInstance, items) {
    
      $scope.items = items;
      $scope.selected = {
        item: $scope.items[0]
      };
    
      $scope.ok = function () {
        $modalInstance.close($scope.selected.item);
      };
    
      $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
      };
    });
    <!doctype html>
    <html ng-app="ui.bootstrap.demo">
      <head>
        <!-- angular 1.4.1 -->
        <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.1/angular.js"></script>
        <!-- angular animate 1.4.1 -->
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.1/angular-animate.min.js"></script>
        <script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.13.0.js"></script>
        <script src="example.js"></script>
        <link href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
      </head>
      <body>
    
    <div ng-controller="ModalDemoCtrl">
        <script type="text/ng-template" id="myModalContent.html">
            <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 ng-click="selected.item = item">{{ item }}</a>
                    </li>
                </ul>
                Selected: <b>{{ selected.item }}</b>
            </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>
    
        <button class="btn btn-default" ng-click="open()">Open me!</button>
        <button class="btn btn-default" ng-click="open('lg')">Large modal</button>
        <button class="btn btn-default" ng-click="open('sm')">Small modal</button>
        <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>

    【讨论】:

      【解决方案6】:

      在您提交按钮或将您移动到另一个页面的任何按钮/选择中,只需使用 data-dismiss="modal" 并且应该处理后退。它只是告诉您在做出选择后关闭模式。

      【讨论】:

        【解决方案7】:

        我也在使用 Angular 1.3.0,我也在使用 UI bootstrap-tpls-0.11.2,由于某种原因,当我重定向到新页面并且背景仍在显示时,我的问题发生了,所以我结束了添加此代码...

            .then(function () {
                $("#delete").on('hidden.bs.modal', function () {
                    $scope.$apply();
                })
            });
        

        我实际上在这里找到的.... Hide Bootstrap 3 Modal & AngularJS redirect ($location.path)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-01-06
          • 1970-01-01
          • 2017-04-23
          • 2015-05-19
          • 2013-10-31
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多