似乎$ state.go对我的应用程序没有任何影响,它什么也没做

时间:2014-02-18 17:12:32

标签: angularjs angular-ui-router

大家好我曾尝试使用角度ui路由器,但在我看来,我不能以任何方式使用$ state.go它只是什么都不做。

我的配置:

    muApp.config([
      '$stateProvider', 
      '$locationProvider',  
      '$urlRouterProvider', 
       function($stateProvider, $locationProvider, $urlRouterProvider) {
    $urlRouterProvider.otherwise('/home');

    $stateProvider.
      state('home', {
        url: '/home',
        templateUrl: 'templates/login.html',
        controller: 'homeCtrl'
      }).
      state('test', {
        url: '/test',
        templateUrl: 'templates/o.html',
        controller: 'testCtrl'
      }).
      state('login', {
        url: '/login',
        templateUrl: 'templates/login.html',
        controller: 'loginCtrl'
      });

    if(window.history && window.history.pushState){
        $locationProvider.html5Mode(true);
    }
  }]);

当我尝试使用$ state.go('login')时,根本没有任何事情发生。

muApp.factory('Auth', ['$rootScope', '$cookies', 'Restangular', '$state', function($rootScope, $cookies, Restangular, $state) {
    function getCurrentUser() {
        if ($rootScope.user) {
            // we already found the user so no need to check with the server again
            return $rootScope.user;
        }
        var login_needed = false;

        //if the user doesnt have this cookie it means he needs to login
        if (!$cookies.userid) {
            login_needed = true;
        }
        // if he does have his cookie lets ask the server if everything ok with this user [and get some info about our use]
        if (!login_needed) {
            Restangular.one('user', $cookies.userid).get().then(function (user) {
                //keep the user data in our scope
                $rootScope.user = user;
                return user;
            }, function() {
                // we failed, probably not authenticated user
                //TODO: shall we alert the user about his not working account?
                login_needed = true;
            });
        }
        if (login_needed) {
            console.log('before');
            $state.go('login');
            console.log('after');
            return null;
        }
    }

    return {
        getCurrentUser: function() {
            return getCurrentUser();
        }
    }

它打印到控制台:

before
after

并且url保持/ home或/ test或之前的任何状态。

我也尝试在控制器本身使用$ state.go,甚至在app.run()中也没用。 我也试过$ state.transitionTo('login'),但它什么都不做:((就像$ state.go

2 个答案:

答案 0 :(得分:3)

好的伙计thx的帮助我改变了我的旧跑块

从:

$rootScope.$on( "$locationChangeStart", function(event, next, current) {
    Auth.getCurrentUser();
});

为:

$rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams){
    console.log('in $stateChangeStart');
    if (toState.name != 'login') {
        event.preventDefault();
        Auth.getCurrentUser();
    }
});

修复了问题:) 似乎没有 event.preventDefault(),网址无法更改

答案 1 :(得分:0)

在这种情况下我要做的是将所有状态事件监听器添加到$ rootscope和console.log中发生的事情,如

$rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams){ 
  console.log('in $stateChangeStart'); 
})

$ stateNotFound $ stateChangeError 以及 $ viewContentLoading

执行相同的操作

还要确保您的'loginCtrl'存在且实际已加载并且模板网址存在

P.S。只是为了踢,当你不启用html5mode

时看看这是否有效