【问题标题】:$location.path takes time to work after bootstrap modal hide (hide.bs.modal) event is fired$location.path 在 bootstrap modal hide (hide.bs.modal) 事件被触发后需要时间来工作
【发布时间】:2017-02-21 10:16:51
【问题描述】:
    // show mymodal
    // register an event to route to a different page once modal is closed
    $('#myModal').on('hide.bs.modal', function() {
        if ($location.path() == '/') {
            $location.path('/ser/mynewpath');
            alert("fired"); // just to check the event was fired
        }
    });
    // open the modal
    $('#myModal').modal('show');

我正在使用 JavaScript 调用引导模式 #myModal。关闭模式后,我想将页面定向到不同的路径。问题是关闭模式后引导页面需要 30 秒 - 1 分钟。我添加了一个警报来检查事件是否迟到触发,但我在关闭模式时收到警报,但页面仅在长时间等待后才被定向。

有时当我点击其他地方时,它会被触发。但这并不总是发生 - 有时漫长的等待是不可避免的。

我最初的猜测是,因为我使用的是引导模式并使用 $location,所以当模式关闭时,角度可能无法检测到它。但现在这似乎是一个愚蠢的猜测。我还尝试将事件触发器更改为“hidden.bs.modal”,但得到了相同的结果。

我将尝试添加 angular $modal 并查看是否可以解决,然后发布更新。但与此同时,我很高兴了解此设置延迟背后的原因。

谢谢。

【问题讨论】:

    标签: javascript html angularjs modal-dialog bootstrap-modal


    【解决方案1】:

    由于是jquery 事件angular 不会立即知道此事件。它将不得不等到下一个摘要周期,这导致了延迟。你尝试使用$scope.$apply

    $scope.$apply(function() {
            if ($location.path() == '/') {
                $location.path('/ser/mynewpath');
                alert("fired"); // just to check the event was fired
            }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-17
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-04
      相关资源
      最近更新 更多