Javascript 登录后重定向到重新加载索引页
我使用angular js完成了简单的登录验证,一旦您登录,它将重定向到配置文件页面后,如果我重新加载我的配置文件页面,它将再次重定向到索引页面(login.html)。我知道这个问题的发生是因为HTML5模式,所以请尝试给出解决方案 HTMLJavascript 登录后重定向到重新加载索引页,javascript,jquery,html,angularjs,Javascript,Jquery,Html,Angularjs,我使用angular js完成了简单的登录验证,一旦您登录,它将重定向到配置文件页面后,如果我重新加载我的配置文件页面,它将再次重定向到索引页面(login.html)。我知道这个问题的发生是因为HTML5模式,所以请尝试给出解决方案 HTML 您需要使用浏览器的存储(会话或本地)保留用户的登录状态。当前,您仅将其存储在内存中,重新加载页面时会擦除内存。您需要使用浏览器的存储(会话或本地)保留用户的登录状态。目前,您只将其存储在内存中,当您重新加载页面时,内存将被擦除。 <!DOCTYPE
您需要使用浏览器的存储(会话或本地)保留用户的登录状态。当前,您仅将其存储在内存中,重新加载页面时会擦除内存。您需要使用浏览器的存储(会话或本地)保留用户的登录状态。目前,您只将其存储在内存中,当您重新加载页面时,内存将被擦除。
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title></title>
<base href="/Angular-workouts/login/index.html" />
<script src="../Angular-lib/angular.min.js"></script>
<script src="../Angular-lib/angular-route.js"></script>
<link href="../Bootstrap-lib/bootstrap.min.css" rel="stylesheet" />
<script src="JavaScript.js"></script>
<style>
body {
font-family:Calibri;
}
</style>
</head>
<body ng-app="myApp" ng-controller="mainController">
<header>
</header>
<div class="container" ng-view></div>
</body>
</html>
<div class="col-md-offset-4 col-md-4" ng-controller="loginController">
<h2>Please Login</h2>
<form role="form">
<div class="form-group">
<label for="email">Username:</label>
<input type="text" class="form-control" ng-model="username" placeholder="Enter Username" >
</div>
<div class="form-group">
<label for="pwd">Password:</label>
<input type="password" class="form-control" ng-model="password" placeholder="Enter password" >
</div>
<button type="button" class="btn btn-default" ng-click="validate()">Login</button>
</form>
</div>
var mylogin = angular.module('myApp', ['ngRoute']);
mylogin.run(function ($browser) {
$browser.baseHref = function () { return "/" };
});
// configure our routes
mylogin.config(function ($routeProvider , $locationProvider) {
$routeProvider
// route for the index page
.when('/', {
templateUrl: '/Angular-workouts/login/login.html'
})
// route for the profile page
.when('/profile', {
resolve: {
"check": function ($location , $rootScope) {
if (!$rootScope.loggedIn) {
$location.path("/");
}
}
},
templateUrl: '/Angular-workouts/login/profile.html'
})
// route for the error page
.otherwise({
templateUrl: '/Angular-workouts/login/error.html'
});
$locationProvider.html5Mode(true);
});
mylogin.controller('mainController', function ($scope) {
});
mylogin.controller('loginController', function ($scope, $location, $rootScope) {
$scope.validate = function () {
if ($scope.username == "admin" && $scope.password == "admin") {
$rootScope.loggedIn = true;
$location.path("/profile");
}
else {
alert("invalid")
}
}
});