【问题标题】:How to close ui-bootstrap modal when press back button on android phone?在Android手机上按下后退按钮时如何关闭ui-bootstrap模式?
【发布时间】:2015-06-29 00:42:03
【问题描述】:

我有一个示例 ui-bootstrap 模式(来自 ui-bootstrap 文档)

angular.module('ui.bootstrap.demo', ['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>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.13/angular.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>

打开一个模态后,当我在android设备上按下返回按钮时,它只是转到上一页,如何关闭模态而不是转到上一页?

我花了很多时间,找到了很多这样的帖子(how to close a bootstrap modal with the browser back button instead of going back a page?`),但是没有用。

有什么解决办法吗?

提前致谢。

【问题讨论】:

    标签: javascript angularjs twitter-bootstrap bootstrap-modal


    【解决方案1】:

    我自己也遇到了这个问题,并找到了一个有效的解决方案here。但它需要使用 ui-router。把它放在你的运行块中:

    .run([
        '$rootScope', '$modalStack',
        function ($rootScope, $modalStack) {
            $rootScope.$on('$locationChangeStart', function (event) {
                var top = $modalStack.getTop();
                if (top) {
                    $modalStack.dismiss(top.key);
                    event.preventDefault();
                }
            });
        }
    ])
    

    【讨论】:

    • 给我错误 Unknown provider: $modalStackProvider
    • 改用 $uibModalStack(可能后来改名了)。
    【解决方案2】:

    您可能希望有一个事件处理程序来获取后退按钮事件。这样,您就有了更多的控制权。我正在使用 Angular Material,但在您的情况下应该非常相似:

    //Use the back button to navigate back
    var backButton = function (ev) {
        var dialogOpen = angular.element(document).find('md-dialog').length > 0;        
        if (dialogOpen) {
            $mdDialog.cancel();
            return false;
        }
    };
    document.addEventListener("backbutton", backButton, false);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-20
      • 2017-06-13
      • 1970-01-01
      相关资源
      最近更新 更多