角UI路由器$ state.go是不是里面的决心重定向
在我angularjs的应用程序,我检查目标网页上,如果用户的土地和已经验证,然后重定向他的主页。
In my angularjs app, I am checking if user lands on landing page and is already authenticated, then redirect him to home page.
.state('landingpage', {
abstract: "true",
url: "/landingpage",
templateUrl: "app/landingpage/landingpage.html",
resolve: {
AutoLoginCheck: ['$state', '$window', function ($state, $window) {
if($window.localStorage.access_token != null)
{
if($window.sessionStorage.access_token == null) {
$window.sessionStorage.access_token = $window.localStorage.access_token;
}
UserInfoService.SetUserAuthenticated(true);
// it is not redirecting
return $state.go('app.home');
}
}]
}
})
问题是,尽管所有的决心code为成功运行,用户没有得到重定向到app.home。谁能告诉为什么发生这种情况?
The problem is that though all the resolve code is successfully run, user is not getting redirected to app.home. Can someone tell why this happens?
注意:国家应用也有它的获取要显示的数据的解析app.home状态
Note: The state 'app' also has a resolve in which it fetches the data to be displayed in 'app.home' state.
有可能是两个解决您的问题。
There can be two solutions to your problem
-
首先,你可以发出一个事件和监听器将处理您的状态转变。您可以实现在一个父控制器随时随地收听
Firstly you can emit an event and the listener will handle your state transition. You can implement the listener in anywhere in a parent controller
其次,你可以实现$ stateChangeStart挂钩,并检查您的重定向状态有
Secondly you can implement the $stateChangeStart hook and check your redirection condition there
$ rootScope。在$('$ stateChangeStart',函数(事件,toState){结果
如果(toState.name ==='的LandingPage'){结果
如果(!isAuthenticated()){//检查用户是否允许转型结果
。事件preventDefault(); // prevent迁移到默认状态结果
$ state.go('home.dashboard');结果
}
}
});
$rootScope.$on('$stateChangeStart', function (event, toState) {
if (toState.name === 'landingpage') {
if (!isAuthenticated()) { // Check if user allowed to transition
event.preventDefault(); // Prevent migration to default state
$state.go('home.dashboard');
} } });