Javascript angularjs$state和$rootScope.$on($stateChangeStart)问题

Javascript angularjs$state和$rootScope.$on($stateChangeStart)问题,javascript,angularjs,angular-ui-router,state,Javascript,Angularjs,Angular Ui Router,State,我已经问了很多问题,但还没有找到解决办法 我对州政府的处理有意见 $urlRouterProvider.otherwise( function($injector, $location) { var $state = $injector.get("$state"); $state.go("cover"); }); $stateProvider .state('auth', { url: '/auth', templateUrl: '../

我已经问了很多问题,但还没有找到解决办法

我对州政府的处理有意见

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

$stateProvider
    .state('auth', {
        url: '/auth',
        templateUrl: '../views/authView.html',
        controller: 'AuthController as auth'
    })
    .state('users', {
        url: '/users',
        templateUrl: '../views/userView.html',
        controller: 'UserController as user'
    })
....
这就是路由在我的应用程序中的工作原理

app.run(function($rootScope, $state, $location) {
    $rootScope.$on('$stateChangeStart', function(event, toState, fromState) {

        //ERROR IS IN THIS BLOCK
        if($rootScope.authenticated && $rootScope.onboard == 0) {

            //event.preventDefault();

            $state.transitionTo("onboard", null, {notify:false});
            $state.go('onboard');
        }
...
每次我更改路径时,该块都会触发数百次,但我希望它只检查用户是否经过身份验证并完成了某些表单,如果没有,则重定向到表单本身


我用
preventDefault()
尝试了不同的方法;或者没有,同样的问题。

首先,我们应该始终尝试检查用户是否已经进入(以前被重定向)状态
'board'
。如果是,停止任何其他处理(只需返回


查看

谢谢兄弟。这真的很有帮助。我在这个问题上花了3个小时。)很高兴看到这一点;)享受强大的用户界面路由器,先生!
$rootScope.$on('$stateChangeStart', function(event, toState, fromState) {

    // in case, that TO state is the one we want to redirect
    // get out of here   
    if(toState.name === "onboard"){
       return;
    }

    if($rootScope.authenticated && $rootScope.onboard == 0) {

        // this should not be commented
        //event.preventDefault();
        // because here we must stop current flow.. .
        event.preventDefault();

        $state.transitionTo("onboard", null, {notify:false});
        $state.go('onboard');
    }
 ...