angularjs - How to redirect in a ui-router resolve? -
i trying redirect inside ui-router resolve , wanted know if there way reroute in router resolver. not work 1 think.
resolver(auth, $state){ if(!auth.isloggedin()){ $state.go('nologgedinpath'); } }
how 1 redirect in resolver correctly ?
my temp hack not comfortable it.
resolver(auth, $state, $timeout){ if(!auth.isloggedin()){ $timeout(function () { $state.go('nologgedinpath'); }, 0); } }
you can return promise resolver
function indicate whether continue navigating state or not. if decide navigate somewhere else - reject promise , specify proper state:
resolver($q, $state, $timeout, auth) { var deferred = $q.defer(); // $timeout example; can xhr request or other async function $timeout(function() { if (!auth.isloggedin()) { // user not logged, not proceed // instead, go different page $state.go('nologgedinpath'); deferred.reject(); } else { // fine, proceed deferred.resolve(); } }); return deferred.promise; }
plunkr here.
upd: updated code , added plunkr. looks works if put inside timeout function.