Angularjs Angular ui router$state.go未在解析内重定向

Angularjs Angular ui router$state.go未在解析内重定向,angularjs,angular-ui-router,Angularjs,Angular Ui Router,在我的angularjs应用程序中,我正在检查用户是否登录到登录页并已通过身份验证,然后将其重定向到主页 .state('landingpage', { abstract: "true", url: "/landingpage", templateUrl: "app/landingpage/landingpage.html", resolve: { AutoLoginC

在我的angularjs应用程序中,我正在检查用户是否登录到登录页并已通过身份验证,然后将其重定向到主页

.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');

                    }
                }]
            }
        })
问题是,尽管所有解析代码都已成功运行,但用户并没有被重定向到app.home。有人能告诉我为什么会这样吗


注意:状态“app”也有一个解析,它获取要在“app.home”状态中显示的数据。

您的问题有两种解决方案

  • 首先,您可以发出一个事件,侦听器将处理您的状态转换。您可以在父控制器的任何位置实现侦听器

  • 其次,您可以实现$stateChangeStart钩子并在那里检查重定向条件

    $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');           
            }
          }
    });
    

您可以使用resolve向控制器提供该州自定义的内容或数据。resolve是一个可选的依赖关系映射,应该将其注入控制器

您可以使用一个控制器来检查AuthState,并相应地进行重定向

     .state('landingpage', {
        abstract: "true",
        url: "/landingpage",
        templateUrl: "app/landingpage/landingpage.html",
        resolve: {
            AutoLoginCheck: ['$window', function ($window) {

                if($window.localStorage.access_token != null)
                {
                    if($window.sessionStorage.access_token == null) {
                        $window.sessionStorage.access_token = $window.localStorage.access_token;
                    }

                   //assuming userInfoService does the authentication
                    var isAuthenticated = userInfoService.SetUserAuthenticated(true);
                    return isAuthenticated;

                }
            }]
        },
        controller: ['$state','AutoLoginCheck', function($state, AutoLoginCheck){
          if(AutoLoginCheck){
            //authenticated
            $state.go('app.home');
          } else {
            //redirect to unauthenticated page
            $state.go('....');
          }
        }]
    })

这对您很有用。

您可以使用
$location.url('/')
来代替。

这是一个旧线程,但我使用$location.path()在状态内完成重定向。resolve()块

无论如何,请解析等待承诺状态。您可以做的最好的事情是返回承诺并为您的状态添加超时:

解析:{
自动登录检查:['$state','$window','$timeout','$q',函数($state,$window,$timeout,$q){
var deferred=$q.deferred();
if(user.isLogin()){
延迟。解决();
}否则{
$timeout(函数(){
$state.go('app.home');
}
拒绝();
}
回报。承诺;
}]

$state与resolve不起作用。为什么以及你能解释什么起作用吗?我相信这可以用与回答
event.preventDefault()相同的方法解决
这个条件应该放在哪里?另外,你还没有注入$state。我不确定这是否有效。它被放在run块中,$rootScope和$state被注入那里
run(function($rootScope,$state){$rootScope.$on('$stateChangeStart',function(event,toState){if(toState.name=='landingpage')){if(!isAuthenticated())event.preventDefault();$state.go('home.dashboard');})
我喜欢这种方法,但假设我需要保护我的路由,并且需要检查他访问的每个路由的用户授权。我需要在任何地方复制它,对吗?我的应用程序中有很多路由。此外,我尝试了这种方法,页面正确重定向,但不是在显示当前页面前一瞬间。有没有办法当控制器重定向时,停止引导到当前状态?我建议在根级抽象状态下具有Auth/ReDebug逻辑,因此所有被保护的状态都将嵌套。但是这将需要更新所有状态。并且为了增加延迟,可以考虑在使用AUTH状态之前使用$TimeOut.CO。你还可以添加一个解释吗?@Robert:Inside resolve唯一可能的事情是拒绝或解决承诺。我认为state.go()是不可能的,除非我们使用timeout在调用堆栈中添加它。我做了类似的操作,但没有$timeout,比如:
return$q.reject().catch(函数(){$state.go('app.home');return$q.reject();})
然后您可以降低injects@Gustav:您的不适用于我,而Shyjal的适用于我。让记录显示@Gustav的答案绝对是最好的。也可以使用
拒绝
发送某种错误对象,并让
$stateChangeError
在我实际想使用的情况下执行重新路由e
$state.go
承诺在重定向后运行一些逻辑。这非常完美。
.state('landingpage', {
            abstract: "true",
            url: "/landingpage",
            templateUrl: "app/landingpage/landingpage.html",
            resolve: {
                AutoLoginCheck: ['$state','$window', '$q','$timeout', function ($state, $window,$q,$timeout) {

                    if($window.localStorage.access_token != null)
                    {
                        if($window.sessionStorage.access_token == null) {
                            $window.sessionStorage.access_token = $window.localStorage.access_token;
                        }
                        UserInfoService.SetUserAuthenticated(true);


                        $timeout(function() {
                           $state.go('app.home')
                        },0);
                        return $q.reject()

                    }
                }]
            }
        })