title: Angular ui-router infinite loop when urlRouteProvider.otherwise is set
body: <p>I'm getting an infinite loop for this code:</p>
  
  <pre><code>    $rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams) {
          var isLoggedIn = (authService.authentication &amp;&amp; authService.authentication.isAuth);
          if (isLoggedIn) {
              //if user is logged and tries to access login page, prevent it
              if (toState.name === 'app.login') {
                  event.preventDefault();
                  return fromState;
              } 
          } else {
              if (toState.name !== 'app.login') {
                  event.preventDefault();
                  $state.go('app.login');
                  return;
              }
          }
      });
  </code></pre>
  
  <p>I also have this set:</p>
  
  <pre><code>function config($stateProvider, $urlRouterProvider, $ocLazyLoadProvider) {
      $urlRouterProvider.otherwise('/main');
  
      $stateProvider
      .state('app', {
          abstract: true,
          url: '',
          controller: 'mainCtrl',
          controllerAs: 'mainCtrl',
          template:'&lt;div ui-view&gt;&lt;/div&gt;'      
      })
      .state('app.login', {
          url: '/logn'
          controller: 'loginCtrl',
          controllerAs: 'loginCtrl',
          templateUrl: 'app/security/login.html'
      })
      .state('app.main', {
          url: '/main'
          controller: 'mainCtrl',
          controllerAs: 'mainCtrl',
          templateUrl: 'app/main.html'
      })
      ...
  }
  </code></pre>
  
  <p>Now i'm getting infinite digest loop when user enters the site and must be authenticated.
  It goes like this:</p>
  
  <ol>
  <li>User enters the address ie: <a href="http://localhost" rel="nofollow">http://localhost</a></li>
  <li>The default route (<strong>otherwise</strong>) fires and reddirects user to <a href="http://localhost/#/main" rel="nofollow">http://localhost/#/main</a></li>
  <li>$<em>OnStateChangeStart</em> fires (for <strong>app.main</strong>) and test if user is logged in, if not then prevent default event end redirect to '<strong>app.login</strong>';</li>
  <li>$<em>OnstateChangeStart</em> fires (for <strong>app.login</strong>) and test if toState.name is app.login' if so, then no need to redirect so return.</li>
  <li>I don't know why but then $<em>OnStateChangeStart</em> fires again but for <strong>app.main</strong>.
  end it lops over and over.</li>
  </ol>
  
  <p>Please help.</p>