gpt4 book ai didi

javascript - 如何检查整个 AngularJS 应用程序的条件

转载 作者:行者123 更新时间:2023-11-30 15:56:23 25 4
gpt4 key购买 nike

如果不是,我想检查用户是否仍然活跃我想将它重定向到登录页面

我在一个 Controller 中进行检查,但不是一个好主意。有人可以帮帮我吗。

这是 Controller

pmaster.controller('AuthenticationCheckControll', ['localStorageService','$scope', '$location', '$window', function (localStorageService, $scope, $location, $window) {

var isValid = localStorageService.get("userID");
// I want this condition be check on every view in this application
if (isValid ==null)
{
$window.location = "/AngularView/html/Master.html#/Login";
}

}]);

最佳答案

你可以简单地在路由中添加下面的代码

$urlRouterProvider.otherwise(function(){
var isValid = localStorageService.get("userID");
if (isValid ==null){
return '/Login';
}else{
// Your home page state url
}
});

或者一个简单的方法是,

  $urlRouterProvider.otherwise(localStorageService.get("userID") ? "/login" : "/homepage");

关于javascript - 如何检查整个 AngularJS 应用程序的条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38462549/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com