Angularjs $ state and $ rootScope. $ on ($ stateChangeStart) problem

I went through a few questions, but did not find a solution.

I have a problem with handling states.

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

how routing works in my application.

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

Each time I change my route, this block is triggered a hundred times, but I want it to simply check whether the user has been checked and if any of his forms have been completed or not, and if he simply is not redirected to the forms themselves.

I tried different ways with preventDefault(); or without, the same thing.

+4
source share
1 answer

-, , ( ) 'onboard'. , ( return).

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

angularjs ui-router

+5

Source: https://habr.com/ru/post/1608215/


All Articles