Why does $state.transitionTo or $state.go does not display the new HTML partial?

后端 未结 3 1625
独厮守ぢ
独厮守ぢ 2021-01-22 09:22

Here is a piece of code destined to check user rights before each UI-router state change. Everything works fine, except the fact that when rights are OK, the transition to the n

3条回答
  •  迷失自我
    2021-01-22 09:36

    I think your problem will be fixed by doing this :

    angular.module('mymodule', [ /* dependancies */ ])
    
    .run( function($rootScope, $window, $state, AuthManager, $timeout)
    {
        $rootScope.$on('$stateChangeStart',
            function(event, toState, toParams, fromState, fromParams)
            {
                if( ! S.isUserConnected() && toParams.requiresLogIn )
                {
    
                    AuthManager.openConnectionPane().then( function( data )
                    {
                        if( AuthManager.isUserConnected() ) {
                            console.log("This message is correctly printed!");
                             $timeout(function() {
                                event.preventDefault();
                                $state.go(toState.name);
                              });
    
                        }
                    });
                }
            }
        );
    });
    

    $state.go does not work correctly in .run this should be a workaround by adding the timeout

提交回复
热议问题