Angular ui-router $state.go is not redirecting inside resolve

后端 未结 6 978
刺人心
刺人心 2021-02-05 04:57

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\', {
                 


        
6条回答
  •  无人共我
    2021-02-05 05:13

    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

    • Secondly you can implement the $stateChangeStart hook and check your redirection condition there

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

提交回复
热议问题