0

My app have the login page, which require to redirect to another extenal web site. as well I require to use the same login page with another new state name.

i have made the required changes, But I am getting the error as

Maximum call stack size exceeded - which I am not able to understand at all. how to overcome with this?

here is my code :

state provider stuff : both are same login controller and html

$stateProvider
      .state('login', {
        url: '/',
        templateUrl: 'app/login/login.html',
        controller: 'loginCtrl as ctrl',
        data: {
          requireLogin: false
        }
      })
      .state('fallback', {
        url: '/fallback',
        templateUrl: 'app/login/login.html',
        controller: 'loginCtrl as ctrl',
        data: {
          requireLogin: false
        }
      })

issue :

function stateController($rootScope,  $state,  fsErr ){
    $rootScope.$on("$stateChangeStart", function(event, toState, toParams, fromState, fromParams ) {


      if( toState.name === 'login' ){

        window.location = 'https://cway.cisco.com/fs/';
        event.preventDefault();

      }

      if( toState.name === 'fallback' ){

        console.log('nothing doing') //consoles 1227 times
        $state.go("fallback");
        event.preventDefault();

        return false;

      }
})
user2024080
  • 1
  • 14
  • 56
  • 96

1 Answers1

0

Maximum call stack size exceeded

This is similar to an infinite loop. The $state.go("fallback"); is getting called infintly and hence causing the error.

Here is the problem: The $stateChangeStart will trigger whenever there is $state changes. So here when the $state is changing to fallback, IF true, then again call $state.go("fallback"); which will trigger stateChangeStart again, and starts looping.

Solution:

  1. You can simply delete the else if block:

    if( toState.name === 'fallback' && !loop) {
        console.log('nothing doing');
        $state.go("fallback");
        event.preventDefault();
        return false;
    } //delete this if statement
    
  2. Set a flag and break the loop.

var loop; // to be defined in parent function outside the $rootScope.$on.

if( toState.name === 'fallback' && !loop) {
    loop = true
    console.log('nothing doing')
    $state.go("fallback");
    event.preventDefault();
    return false;
}
Amir Suhail
  • 1,284
  • 3
  • 12
  • 31