Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ui-router - $state.go() not working

Here is the app.js of my project:

(function () {
'use strict';

angular
    .module('app', ['ui.router', 'ngCookies', 'angular-inview', 'ngMaterial'])
    .config(config)
    .run(run);

config.$inject = ['$stateProvider', '$urlRouterProvider', '$mdThemingProvider'];
function config($stateProvider, $urlRouterProvider, $mdThemingProvider) {
        $mdThemingProvider.theme('default')
            .primaryPalette('deep-orange')
            .accentPalette('teal', {
              'default': 'A400'
            });

        $urlRouterProvider.otherwise('/app');

        $stateProvider
            .state('app', {
                url: '/app',
                data: {
                    clearenceLevel: 1
                  },
                  views: {
                    '': {
                        templateUrl: 'app/views/navbar.html',
                    }
                }
            })
            .state('home.works', {
                url: '/works',
                templateUrl: 'app/views/works.html',
                controller: 'WorksController as vm'
            })
            .state('login', {
                url: '/login',
                templateUrl: 'app/views/login.html',
                controller: 'LoginController as vm',
                data: {
                    clearenceLevel: 0
                  }
            })
            .state('register', {
                url: '/register',
                templateUrl: 'app/views/register.html',
                controller: 'RegisterController as vm',
                data: {
                    clearenceLevel: 0
                  }
            });
}
run.$inject = ['$rootScope', '$location', '$state', '$cookieStore', '$http', 'AuthenticationService'];
function run($rootScope, $location, $state, $cookieStore, $http, AuthenticationService) {
    $rootScope.globals = $cookieStore.get('globals') || {};

    if ($rootScope.globals.currentUser) {
        $http.defaults.headers.common['aimm-token'] = $rootScope.globals.currentUser.token;
        $http.defaults.headers.common['aimm-id'] = $rootScope.globals.currentUser.id;
    }

    $rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) {
        var clearenceLevel = toState.data.clearenceLevel;
        var loggedIn = $rootScope.globals.currentUser;

        if (clearenceLevel > 0 && !loggedIn) {
            alert("redirecting");
            return $state.go('login');
        }
    });
}
})();

I just can't have $state.go() working. The $on('$stateChangeStart'...); is working fine, and the alert is poping when trying to reach a protected state with no session. But the return $state.go('login'); doesnt work. It redirects to /app.

Thanks for your help.

like image 202
Ruben Renzema Avatar asked Jul 16 '15 09:07

Ruben Renzema


1 Answers

Well, thanks to @Vanojx1, I found out adding e.preventDefault(); before the $state.go('login'); made it work. Still dont understand why though.

like image 198
Ruben Renzema Avatar answered Oct 18 '22 09:10

Ruben Renzema