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.
The session is checked using a custom service and the state redirect should happen in it's callback. This all happens in a function init at the page initialization.
It doesn't work though because the ui-router 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?
index.html
<!DOCTYPE html> <html lang="en"> [...] <body ng-app="name" ng-controller="mainController" ng-init="init()" ui-view> </body> </html> mainController
.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(); } }); }; }) EDIT: Route definitions
.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: {...} }) })
app.config()