I have an AngularJS application with UI-Router. In my app.js file I have the following code (where AuthFact is a custom factory for authentication utility):
var testapp = angular.module('testapp', ['ui.router']);
testapp.config(function($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise('/app/products');
$stateProvider
.state('app', {
url: '/app',
abstract: true,
templateUrl: 'Views/App.html',
})
.state('app.products', {
url: '/products',
templateUrl: 'Views/Product/Product.html',
controller: 'ProductController',
authenticate: true
})
.state('login', {
url: '/login',
templateUrl: 'Views/Login.html',
authenticate: false
})
});
testapp.run(function ($rootScope, $state, AuthFact) {
$rootScope.$on("$stateChangeStart",
function (event, toState, toParams, fromState, fromParams) {
if (!AuthFact.IsAuthenticated() && toState.authenticate == true) {
$state.go('login');
event.preventDefault();
}
});
});
When I run my application, it tries to go to the /products url because the otherwise route, but because the user isn't logged it redirect to the /login page.
The problem is that i have the following error:
Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting! Watchers fired in the last 5 iterations: []