I am trying to redirect the user to the login page if local storage clears for any reason. (Example: Perhaps they opened the app again in another tab and logged out).
I have the following code in my header directive:
$scope.$watch(
function (scope) {
if (!$localStorage.user || !$localStorage.user.hasOwnProperty('basicAuth'))
$state.go('login');
$scope.user = $localStorage.user;
return $localStorage.user;
}
);
There was a $watch watching the $localStorage so I decided to put my logic in there.
But, it causes the following Error:
Line: 16033 Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting! Watchers fired in the last 5 iterations: [] http://errors.angularjs.org/1.4.6/$rootScope/infdig?p0=10&p1=%5B%5D
To my knowledge, the login page doesn't load the header directive. Conceptually, I don't see anything wrong with my logic but obviously angular doesn't like it. Maybe I'm already headed for the login page and I'm saying go there even though I'm already headed there.
What am I doing wrong?