尝试在角度启动时将用户路由到特定状态,但是如果它是真的则不会登录,而不是转到默认的routState
.run(function ($ionicPlatform, $rootScope, $state) {
var authToken = window.localStorage.getItem('authToken')
var shouldChangePassword=window.localStorage.getItem('shouldChangePassword')
if (authToken == 'undefined' || shouldChangePassword == 'true') {
event.preventDefault();
$state.go('login');
return;
}
.config(
//my routes
.state('login', {
url: '/login',
templateUrl: 'templates/account_login.html',
controller: 'loginCtrl',
})
$urlRouterProvider.otherwise('/tab/category');
)
如何重定向到登录并停止应用程序进入默认路由
答案 0 :(得分:0)
您可以尝试挂钩UI-router's $stateChangeStart状态更改事件:
.run(function ($ionicPlatform, $rootScope, $state) {
$rootScope.$on('$stateChangeStart', function (event) {
var authToken = window.localStorage.getItem('authToken');
var shouldChangePassword=window.localStorage.getItem('shouldChangePassword');
if (!authToken || shouldChangePassword) {
event.preventDefault();
$state.go('login');
}
});
但您可以考虑将authToken和shouldChangePassword移动到单独的服务,并从/向该服务读取/设置值,而不是从localstorage读取每个状态更改