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


here app.js of 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 vm'             })             .state('login', {                 url: '/login',                 templateurl: 'app/views/login.html',                 controller: 'logincontroller vm',                 data: {                     clearencelevel: 0                   }             })             .state('register', {                 url: '/register',                 templateurl: 'app/views/register.html',                 controller: 'registercontroller 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 can't have $state.go() working. $on('$statechangestart'...); working fine, , alert poping when trying reach protected state no session. return $state.go('login'); doesnt work. redirects /app.

thanks help.

well, @vanojx1, found out adding e.preventdefault(); before $state.go('login'); made work. still dont understand why though.


Comments