【问题标题】:module.run() and $state.go() angularJSmodule.run() 和 $state.go() angularJS
【发布时间】:2014-10-14 07:39:04
【问题描述】:

我可能有某种误解。我正在使用 Angular ui 路由器,我有下一个问题:

我有下一个州提供者:

angular
.module('MainApp')
.config(['$stateProvider', '$urlRouterProvider', function ($stateProvider, $urlRouterProvider ) {
    $stateProvider
        .state('register', {
            url: "/",
            abstact: true,
            views: {
                "side-bar": {
                    controller: "GetSideBarCtrl"
                },
                "page-body@register": {
                    templateUrl: "/App/Views/WelcomePage.html"
                }
            },
        })
        .state('register.Register', {
            url: "Register",
            views: {
                "page-body": {
                    controller: "RegisterCtrl",
                    templateUrl: "/App/Views/Register.html"
                }
            },
        })
        .state('register.Login', {
            url: "Login",
            views: {
                "page-body": {
                    controller: "LoginCtrl",
                    templateUrl: "/App/Views/Login.html"
                }
            },
        })
        //For registered and loged in users!
    /* */
        .state('main', {
            url: "/:userId",
           // abstact: true,
            views: {
                "side-bar": {
                    controller: "GetSideBarCtrl"
                },
                "page-body@main": {
                  //  controller: "LoginCtrl",
                    templateUrl: "/App/Views/MainPage.html"
                }
            },
        });
    $urlRouterProvider.otherwise('/');
}]);

并运行函数

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]);
        $state.go('main', { userId: cookies.split(' ')[0] });
    } else {
        $state.go('register.Login');
    }
}]);

一切正常,当 cookie 存在并且身份验证工作没有任何问题但 $state.go 什么都不做时,我不知道为什么,请你帮忙解释一下...

【问题讨论】:

    标签: javascript angularjs


    【解决方案1】:

    是的,正如建议的那样,$state.go() 在 module.run() 中似乎无法正常工作。

    我发现将 $state 代码放入 $timeout 是可行的,例如

    angular.module('MainApp').run($timeout, $state, ...) {
    
        $timeout(function() {
            $state.go('main');
        });
    
    }
    

    【讨论】:

      【解决方案2】:

      所以我让它以不同的方式工作,我使用了 $stateChangeStart,现在我的运行函数如下所示:

      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 });
                      }
                  });
          }
      ]);
      

      但我仍然不知道为什么 $state.go() 不起作用...

      【讨论】:

      • 重要的补充是event.preventDefault()。没有它,当前的转换(transitionTo() 承诺)将不会停止/中止。见:github.com/angular-ui/ui-router/wiki#state-change-events
      • @SatisfyMe,请从 app.config() 中删除您的声明 $urlRouterProvider.otherwise('/');。然后$state.go() 就可以了。
      【解决方案3】:

      我遇到了完全相同的问题。
      我正在使用 ionic 构建一个跨平台的移动应用程序。
      我有类似 --> 如果用户已经登录的逻辑。重定向到主页,否则重定向到登录页面

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

      这在 Android 上完美运行,但在 IOS 上偶尔会崩溃。经过一番调查研究。我使用 $location.path 而不是 $state.go 并且一切正常。

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

      希望这能帮助您解决问题。

      【讨论】:

        【解决方案4】:

        $state.go 在运行中不起作用,它还没有初始化!我所做的是将逻辑放入服务中,将控制器添加到您运行的模块中,然后在控制器中运行它!应该可以的。

        【讨论】:

          【解决方案5】:

          只要指出遵循this thread 的建议,您也可以通过更改 else 块来解决它:

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

          希望对你有帮助

          【讨论】:

            【解决方案6】:

            我遇到了类似的问题。 $state.go 在 .run 函数中不起作用。

            将 $state.go 放在服务中解决了我的问题。

            【讨论】:

              【解决方案7】:

              这听起来很奇怪,但对我来说,解决方案是重命名我的状态并删除点...

              我想要一个针对客户、主管、经理或其他任何人的不同主页(但使用相同的 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');
                       }
                    }
              }
              

              这不起作用,将名称 home.customer state 替换为 home-customer$state.go('home-customer'); 就可以了...

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2014-08-22
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2016-03-27
                • 2015-07-12
                相关资源
                最近更新 更多