module.run() and $state.go() angularJS

后端 未结 7 2240
猫巷女王i
猫巷女王i 2021-02-12 12:38

I might have some kind of missunderstanding. Im using angular ui router and i have the next issue:

I have the next State provider:

angular
.module(\'Main         


        
相关标签:
7条回答
  • 2021-02-12 12:42

    I am having exactly the same problem.
    I am using ionic to build a cross-platform mobile app.
    I have logic like --> if the user is already logged in. redirect to the main page, otherwise redirect to login page

    if(isLogin("userId")){
        $state.go("main");
    }else{
        $state.go("login");
    }
    

    This work perfectly on Android but crash occasionally on IOS. After some investigation and research. I use $location.path instead of $state.go and everything works fine.

    if(isLogin("userId")){
        $location.path("/main");
    }else{
        $location.path("/login");
    }
    

    Hope this help you solve your problem.

    0 讨论(0)
  • 2021-02-12 12:54

    Just point that following the advises of this thread, you can also solve it changing the otherwise block for this:

      $urlRouterProvider.otherwise(function ($injector, $location) {
        var $state = $injector.get("$state");
        $state.go("/");
      });
    

    Hope it helps

    0 讨论(0)
  • 2021-02-12 13:01

    It can sound strange but for me the solution was to rename my state and remove the dot ...

    I want an home page different for customer, director, manager or anything ( but with the same url )

      .state('home', {
        url : '/',
        templateUrl: 'views/home.html',
        controller: 'HomeCtrl',
        controllerAs: 'home',
        module :'private'
      })
      .state('home.customer', {
        url : '',
        templateUrl: 'views/home-customer.html',
        controller: 'HomeCustomerCtrl',
        controllerAs: 'homeCustomer',
        module :'private'
      })
    
      .run(function ($rootScope, $state, $window,userService) {
        $rootScope.$on("$stateChangeStart", function(e, toState, toParams, fromState, fromParams) {
         if(toState.name === 'home') {
              if(userService.isCustomer()) {
                   $state.go('home.customer');
             }
          }
    }
    

    This dosen't work, replace the name home.customer state with home-customer and $state.go('home-customer'); and it works ...

    0 讨论(0)
  • 2021-02-12 13:03

    So i've made it work the different way, ive used $stateChangeStart and now my run function looks like this:

    angular
    .module('MainApp')
    .run(['$rootScope', '$state', '$cookieStore', 'principal',
        function ($rootScope, $state, $cookieStore, principal) {
            var cookies = $cookieStore.get('user');
            if (cookies) {
                principal.Authenticate(cookies.split(' ')[0], cookies.split(' ')[1]);
            }
            $rootScope.$on('$stateChangeStart',
                function (event, toState) {
                    if ((toState.name === "register") && (principal.isAuthenticated)) {
                        event.preventDefault();
                        $state.go('main', { userId: principal.identity().userId });
                    }
                });
        }
    ]);
    

    But i still dont know why $state.go() didnt work...

    0 讨论(0)
  • 2021-02-12 13:05

    I experienced a similar problem. $state.go not working in the .run function.

    Placing $state.go in a service solved my problem.

    0 讨论(0)
  • 2021-02-12 13:09

    Yep, as suggested it looks like $state.go() doesn't work correctly in module.run().

    I found that putting the $state code in a $timeout works, e.g.

    angular.module('MainApp').run($timeout, $state, ...) {
    
        $timeout(function() {
            $state.go('main');
        });
    
    }
    
    0 讨论(0)
提交回复
热议问题