title: ui-router check state on page load
body: <p>On page load my application checks if the user still has a valid session on the server. If the user is still logged in the application should redirect him to his dashboard if not already in a (sub)state if it.</p>
  
  <p>The session is checked using a custom service and the state redirect should happen in it's callback. This all happens in a function <code>init</code> at the page initialization.</p>
  
  <p>It doesn't work though because the <code>ui-router</code> state seens to still be empty during this point of execution. How can I call the function doing all this so the state is initialized then? When exactly is the state initialized?</p>
  
  <p><strong><code>index.html</code></strong></p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
      [...]
      &lt;body ng-app="name" ng-controller="mainController" ng-init="init()" ui-view&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><strong><code>mainController</code></strong></p>
  
  <pre><code>.controller('mainController', function ($scope, $state, session, [...]) {
      $scope.init = function () {
          session.isLoggedIn().success(function (response) {
              if (response) {
                  if (!$state.includes('dashboard')) {
                      $state.go('dashboard');
                  }
              } else {
                  $scope.logout();
              }
          });
      };
  })
  </code></pre>
  
  <p><strong>EDIT: Route definitions</strong></p>
  
  <pre><code>.config(function ($stateProvider, $urlRouterProvider) {
      $urlRouterProvider.otherwise('/');
      $stateProvider
          .state('loading', {
              url: '/',
              template: '...'
  
          })
          .state('login', {
              url: '/login',
              views: {...}
          })
          .state('dashboard', {
              url: '/dashboard',
              views: {...}
          })
          .state('dashboard.sub', {
              url: '/sub/{param:int}',
              views: {...}
          })
  })
  </code></pre>