I have an app that does login and has to make decisions on whether or not the user can go to certain pages if they are logged in or not. The issue i'm having is that upon hitting a certain route, I don't need a template, but I call a service to handle sending them to the login page or letting them continue. The service checks to see if they're logged in and if not if uses window.location.href to send them to the login route.
When it hits the window.location.href line, the login controller is infinitely initialized (10 $digest() iterations reached) and nothing works after that point. Am I missing something as far as redirecting goes with hashes?
This only happens in IE9. The looping issue doesn't happen in chrome nor safari.
The routing code:
angular.module('myApp', []).config(['$routeProvider', function($routeProvider) {
$routeProvider
.when('/login', {templateUrl: 'partials/login.html', controller: 'LoginCtrl', title:'Login'})
.when('/register', {template : " ", controller: 'RegisterCtrl', title:'Register'})
.when('/eBooks', {resolve: {redirect: 'LoginRouter'}});
}]);
The service for handling login routing:
.service('LoginRouter', ['RouteService', '$rootScope', '$window', function(RouteService, $rootScope, $window) {
if (!$rootScope.isLoggedIn) {
RouteService.setModule("eBooks");
$window.location.href = "#/login"; // Causing infinite loop
}
else {
var config = $rootScope.config;
$rootScope.startLoading();
$window.location.href = config.EBOOK_URL+ "&bookId=" + config.bookId.replace("-","");
}
}]);
The login controller:
.controller('LoginCtrl',['$scope' ,'LoginService', function ($scope, LoginService) {
$scope.user = {mtn: ""};
$scope.checkUser = function() {
LoginService.validateUser($scope);
};
}]);
NOTE: I changed the code just to point to an html template with an anchor tag and href pointing to "login" and it worked just fine. It's only when I fire location change through window directly that it gets stuck in the loop.
UPDATE: I removed everything from the login.html template to see if something in the html was causing the issue, removed all directives, and any functions/models on the controller scope and rootScope and still the issue happens.
UPDATE 2 I tried using angular to force a click on an anchor to try and bypass the issue but it still happens. A funny thing I noticed is that if I have the console window open in IE then the issue doesn't even happen in the first place. What is going on????
window.location.hashinstead? That's guaranteed (on IE9 at least) not to reload the page.