AngularJS:如何在会话超时时关闭活动的 $modal
AgularJS: How to close active $modal on session timeout
我正在开发 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');
因此,为了在重定向后在一个地方关闭所有打开的模式,您可以订阅
$routeChangeSuccess
事件 $rootScope
并使用 $modalStack.dismissAll();
app.run(['$rootScope', '$modalStack', function ($rootScope, $modalStack) {
$rootScope.$on('$routeChangeSuccess', function () {
$modalStack.dismissAll();
});
}]);
我正在开发 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');
因此,为了在重定向后在一个地方关闭所有打开的模式,您可以订阅
$routeChangeSuccess
事件 $rootScope
并使用 $modalStack.dismissAll();
app.run(['$rootScope', '$modalStack', function ($rootScope, $modalStack) {
$rootScope.$on('$routeChangeSuccess', function () {
$modalStack.dismissAll();
});
}]);