我遇到问题,登录后我的页面没有重定向到主页面。我使用$window.location
时检查了状态登录工作情况。我想使用angular-route
来阻止没有登录的用户访问我的主菜单。如果我使用$window.location
,用户无需登录即可访问我的主菜单。这是我的js文件
app.config(function($routeProvider){
$routeProvider
.when('/',{
templateUrl:'index.html'
})
.when('/mainmenu',{
resolve:{
"check":function($location,$rootScope){
if (!$rootScope.loggedIn) {
$location.path('/');
}
}
},
templateUrl:'content/mainmenu2.html'
})
.otherwise({
redirectTo:'/'
});
});
app.controller("logincont", ['$scope','$http','$window','$rootScope','$location',function($scope,$http,$window,$rootScope,$location){
$scope.login = function () {
$http.get('http://localhost:8089/MonitoringAPI/webresources/login?a='+$scope.userid+'&d='+$scope.password).then(function(response){
$scope.reslogin = response.data;
if($scope.reslogin == null)
{
alert('Login Incorrect');
}
else
{
$rootScope.loggedIn = true;
$location.path('/mainmenu');
}
});
};
}]);
这是我的HTML
<form>
<h1 class="tengah"> Form Login </h1>
<div class="form-group">
<label for="inputId">User Id</label>
<input type="text" class="form-control" ng-model="userid" placeholder="userid" required>
</div>
<div class="form-group">
<label for="inputPassword">Password</label>
<input type="password" class="form-control" ng-model="pass" placeholder="Password" required>{{password}}
</div>
<button type="submit" class="btn btn-primary" ng-click="login()">
<span class="fa fa-sign-out"></span>Login</button>
</form>
答案 0 :(得分:2)
您可以在您不想对例如allowUnauth
进行身份验证的路线上使用标记login
,然后更改路线,如下所示: -
app.config(function($routeProvider){
$routeProvider
.when('/',{
templateUrl:'index.html',
allowUnauth:true
})
.when('/mainmenu',{
templateUrl:'content/mainmenu2.html'
}).otherwise({
redirectTo:'/'
});
});
var unauthRoutes= [];
angular.forEach($route.routes, function(route, path) {
// push route onto unauthRoutes if it has a truthy allowUnauth value
route.allowUnauth && (unauthRoutes.push(path));
});
$rootScope.$on('$routeChangeStart', function(event, nextLoc, currentLoc)
{
var atuhRoute= (-1 === unauthRoutes.indexOf($location.path()));
if(atuhRoute && !$rootScope.loggedIn) {
$location.path('/');
}
});
答案 1 :(得分:0)
尝试类似
的内容"use strict";
configureApp.$inject = ['$routeProvider'];
function configureApp($routeProvider) {
$routeProvider
.when('/mainmenu', {
resolve: {
ensureAthenticated: (function() {
ensureAthenticated.$inject = ['$rootScope'];
function ensureAthenticated($rootScope) {
if(!$rootScope.loggedIn) {
return Promise.reject('not athenticated');
}
else return Promise.resolve();
};
return ensureAthenticated;
}())
},
templateUrl:'content/mainmenu2.html'
})
.otherwise('/');
}
app.config(configureApp);