Reputation: 4211
I'm getting an infinite loop for this code:
$rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams) {
var isLoggedIn = (authService.authentication && 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;
}
}
});
I also have this set:
function config($stateProvider, $urlRouterProvider, $ocLazyLoadProvider) {
$urlRouterProvider.otherwise('/main');
$stateProvider
.state('app', {
abstract: true,
url: '',
controller: 'mainCtrl',
controllerAs: 'mainCtrl',
template:'<div ui-view></div>'
})
.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'
})
...
}
Now i'm getting infinite digest loop when user enters the site and must be authenticated. It goes like this:
Please help.
Upvotes: 2
Views: 4459
Reputation: 4014
Change the otherwise()
method declaration to a state name transition instead of a URL transition which you are currently using. This solved it for me when I experienced this problem.
Heres an example:
$urlRouterProvider.otherwise(function($injector, $location){
$injector.invoke(['$state', function($state) {
$state.go('app.main');
}]);
});
An alternate way of getting the $state
and executing the $state.go
would be
var $state = $injector.get('$state');
$state.go('app.main');
I'm not very experienced in Angular so I don't know why this solves it so if anybody can elaborate please do!
Update
The accepted answer in this SO post seems to provide a good explanation on why this happens.
Upvotes: 12