I'm trying to introduce login into the way the user navigates accross the application.
I pretend to redirect the user to the page were he was before he navigate to the login page if that page meets specific requirements
Preventing the event from the $stateChangeStart stop's the state change like expected but when i run the $state.go('into_somewhere') i enter an infinit loop
My angular version is 1.3.1 and the ui-router is the latest
.factory('RouteHistory', function ($rootScope,$log, $state, Auth, $urlRouter, $timeout) {
// after the user enter a page
var currentState = '';
// when the user is trying to access a page that he has not permissions
// or that requires the user to be logged in
var pendingState = '';
var isMenuTogglerVisible = false;
var skipFromStateVal = true;
$rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams){
event.preventDefault();
if (toState.name == 'login' && fromState.name != 'login'){
$log.log('Ui-router: changing to login');
// $urlRouter.sync();
$state.go('login')
//pendingState = fromState;
//$log.log('Peding state updated to:' + pendingState.name );
//$urlRouter.sync();
}
if (fromState.name == 'login' && Auth.isLoggedIn()) {
$log.log('Ui-router: going from login');
//$state.go(fromState.name);
$timeout(function(){
// $state.go('home', null, {/*reload: true, location: 'replace'*/});
$state.go('browse-machine');
//$urlRouter.sync();
},2000)
}
$log.log({
'toState': toState,
'toParams': toParams,
'fromState': fromState,
'fromParams': fromParams
})
})
return {
};
});
In general I would say, let's redirect (
$state.go()
) only if needed. In other cases, get out from the event listener:This is simplified logic, but shows, that we should change to execution only if needed. There are some other examles with more detailed implementation and plunkers:
As provided in the comment, there was plunker, which I changed like this here
And this is not looping anymore. Please, check it here
This answer helped me:
Original: Why does AngularJS with ui-router keep firing the $stateChangeStart event?
The infinite loop is partly caused by
..which says
if
you're going to the login state,then
go to the login state....And calling
event.preventDefault()
as the first line in the event handler doesn't help. When you usego()
to go to the login screen (or anywhere else), that state change is also prevented byevent.preventDefault()
. It should only be used within anif
.Your entire $stateChangeStart handler should instead be...
...which reads naturally. "
If
you're not logged in and you're not already going to the login screen,then
stop what you're doing, I'll remember where you wanted to go, and you now go to the login screen."Later your
Auth
object will issue a$state.go(Auth.desiredState)
when it's satisfied with the user.You should use the notify option :
This will prevent stateChangeStart to fire again.
It works for me, Below code helps to get rid of infinite loop
I simply used
$location.path('every/where')
instead of$state.go('every/where')
:) .