Angularjs AgularJS:如何在会话超时时关闭活动$modal

Angularjs AgularJS:如何在会话超时时关闭活动$modal,angularjs,session-timeout,angularjs-routing,bootstrap-modal,Angularjs,Session Timeout,Angularjs Routing,Bootstrap Modal,我正在开发一个SPA,在会话超时时,我会将用户重定向到登录页面。这是我的实现 .run(['$rootScope', '$state', '$location', function ($rootScope, $state, $location) { $rootScope.$watch(function detectIdle() { var now = new Date(); if (now - lastDigestRun > 20 * 60 * 1000)

我正在开发一个SPA,在会话超时时,我会将用户重定向到登录页面。这是我的实现

.run(['$rootScope', '$state', '$location', function ($rootScope, $state, $location) {

    $rootScope.$watch(function detectIdle() {

    var now = new Date();

     if (now - lastDigestRun > 20 * 60 * 1000) {

        $location.path('/login');

        setTimeout(function () {
          alert("Your session has expired. Please log in again");
        }, 1000); 
     }
   });

}])
问题:如果某个$modal打开时应用程序超时,则页面重定向到登录页面,但$modal不会关闭

请帮我解决这个问题

谢谢

更新:很抱歉回答我自己的问题,工作解决方案如下:

 .run(['$rootScope', '$state', '$location', function ($rootScope, $state, $location) {

    var isSessionTimeout = false; 

    $rootScope.$watch(function detectIdle() {

    var now = new Date();

     if (now - lastDigestRun > 20 * 60 * 1000) {

        isSessionTimeout = true;
        $location.path('/login');

        setTimeout(function () {
          alert("Your session has expired. Please log in again");
        }, 1000); 
     }
   });

  $rootScope.$on('$stateChangeSuccess', function () {
    if (isSessionTimedOut) {
        $modalStack.dismissAll();
    }             
 });

}])

您可以通知会话以以下方式结束:

$rootScope.$broadcast('sessionEnd');
在所有模态的控制器中:

$scope.$on('sessionEnd', function() {
    $modalInstance.dismiss();
});

会话过期时,您将用户重定向到使用登录

 $location.path('/login');
因此,为了在重定向后在一个地方关闭所有打开的modals,您可以在 $
routeChangeSuccess
事件的$
rootScope
并使用
$modalStack.dismissAll()


谢谢你的建议。我对AngularJS很陌生,现在我明白$broadcast是如何工作的了。谢谢Alex和Mart,你们的两个解决方案都对我有用@Alex而不是$routeChangeSuccess我使用$StateChangeSucture,但它只起作用。这是因为您使用AngularUI路由器进行路由,而不是Angular中的默认ngRoute模块。这里的
$modalStack
是什么?
 app.run(['$rootScope', '$modalStack', function ($rootScope, $modalStack) {
    $rootScope.$on('$routeChangeSuccess', function () {
        $modalStack.dismissAll();
    });
 }]);