1

If I put notify to true, does not render the login.html, is a blank page. If I put as false, but returns renders me several errors in the console:

RangeError: Maximum call stack size exceeded
    at $.resolve (angular-ui-router.min.js:1)
    at b (angular-ui-router.min.js:1)
    at Object.x.transitionTo (angular-ui-router.min.js:1)
    at Object.x.go (angular-ui-router.min.js:1)
    at app.js:317
    at p.$broadcast (angular.min.js:3)
    at Object.x.transitionTo (angular-ui-router.min.js:1)
    at Object.x.go (angular-ui-router.min.js:1)
    at app.js:317
    at p.$broadcast (angular.min.js:3)

My code router:

$rootScope.$on('$stateChangeStart', 
   function(event, toState, toParams, fromState, fromParams) {

        var shouldLogin = !$window.sessionStorage.token && !AuthenticationService.isLogged;

        $rootScope.Authenticated = shouldLogin;

        //redirect only if both isAuthenticated is false and no token is set
        // if (!AuthenticationService.isLogged && !$window.sessionStorage.token) {
        //     $state.go('login');
        // }

        //console.log($rootScope.Authenticated);

        if(shouldLogin) {
            $state.go('login', {}, {notify: false, location: false});
            event.preventDefault();
            return;
        }

    });
Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
Marco Riesco
  • 401
  • 1
  • 6
  • 17
  • 1
    The Maximum call stack usually occurs when you have and infinite callback. Try to debug your code thinking about this aspect. – Rodmentou Nov 04 '15 at 17:48
  • 2
    don't use `$state.go` here. that causes another `$stateChangeStart`, which causes this function to be evaluated again, which causes another `$state.go`, etc. – Claies Nov 04 '15 at 17:52
  • I resolved using $location. – Marco Riesco Nov 04 '15 at 17:59

1 Answers1

1

I would say, that you can go your way, just ... add a line to NOT redirect, when already going to login:

$rootScope.$on('$stateChangeStart', 
  function(event, toState, toParams, fromState, fromParams) {

    var alreadyGoingToLogin = toState.name === "login";
    if(alreadyGoingToLogin) {
      return
    }
    ...

Check these with working examples:

Community
  • 1
  • 1
Radim Köhler
  • 122,561
  • 47
  • 239
  • 335