我是 angularjs 平台的新手。我正在创建一个log-in application
. 我面临一个问题,所有事情都正常,但陷入管理,如果用户已经登录并尝试在不同的选项卡中打开相同的 url,它会将用户直接重定向到dashbord page(dashboard.html)
,但是当我按下时browser back button
,而不是去上一个页面,它打开login page(login_admin.html)
。场景如下:
localhost:8080 --> 它打开 --> login_admin.html(已实现)。
输入凭据并提交->打开->dashboard.html(已实现)。
在不同的选项卡中输入相同的 url(localhost:8080)(假设以前打开 facebook.com)---> 打开 --> dashbord.html(已实现)。
在按下浏览器后退按钮时
facebook.com
,它会打开 login_admin.html(未解决),而不是 go。请提出一些解决方案。
我的js代码如下:
var app=angular.module('myApp', ['ngRoute','ngCookies']);
console.log("in appnew.js")
app.config(function($routeProvider,$locationProvider) {
$locationProvider.html5Mode(true);
$routeProvider
.when('/',{
templateUrl: 'login_admin.html',
controller: 'userController'
})
.when('/dashboard',{
templateUrl: 'dashboard.html',
controller: 'userController',
authenticated: true
})
.when('/logout',{
templateUrl: 'logout_admin.html',
controller: 'userController'
})
.otherwise({
redirectTo: "/"
});
});
app.controller('userController',function($scope,$location,userModel){
angular.extend($scope,{
login: function(adminfrm){
var data={
jeeb_no: $scope.admin.name,
password: $scope.admin.password
};
userModel.login(data).then(function(){
$location.path('/dashboard');
});
},
logout: function(){
userModel.doUserLogout();
$location.path('/');
}
});
});
app.run(["$rootScope",'$location','userModel', function($rootScope,$location,userModel,$window){
$rootScope.$on('$routeChangeSuccess', function(event, next, current){
console.log("event: %j",event);
console.log("next: %j",next);
console.log('current: %j',current);
if (next.$$route.authenticated) {
console.log("next.$$route.authenticated"+next.$$route.authenticated);
console.log('userModel.getAuthStatus app.run if 1'+userModel.getAuthStatus());
if (!userModel.getAuthStatus()) {
console.log("getAuthStatus"+userModel.getAuthStatus);
console.log('userModel.getAuthStatus app.run if 1(1)'+userModel.getAuthStatus());
$location.path('/');
}
}
if (next.$$route.originalPath =='/') {
console.log("next.$$route.originalPath "+next.$$route.originalPath);
if (userModel.getAuthStatus()) {
console.log("current "+current);
console.log("next "+next);
next.$$route.originalPath = '/dashboard'
$location.path(next.$$route.originalPath);
}
}
});
}]);
app.factory('userModel', function($http,$cookies,$location){
var userModel={};
userModel.login= function(loginfrm){
data={
jeeb_no: loginfrm.jeeb_no,
//password: loginfrm.jeeb_no
};
$cookies.put('auth',data);
console.log("loginfrm"+loginfrm);
return $http.post("http://1/admin_login", data).
success(function(response){
/*console.log('$scope.dynamic1: %j', $scope);*/
console.log("response success: %j",response)
}).
error(function(response){
console.log("response error:",response);
});
};
userModel.getAuthStatus = function(){
var status = $cookies.get('auth');
console.log('status: %j', status);
if(status){
return true;
}
else{
return false;
}
};
console.log('userModel.getAuthStatus'+userModel.getAuthStatus());
userModel.doUserLogout = function(){
$cookies.remove('auth');
}
console.log("userModel: %j",userModel);
return userModel;
})