2

I have tried both $locationChangeStart and $routeChangeStart, but none of them work - they either cause flickering (switchign urls) or don't work at all. what am I doing wrong here?

$rootScope.$on('$locationChangeStart', function (event,future,current) {
    if (!user.isAuthenticated()){
        $location.path('/login');
        event.preventDefault();
    }
  });

 

$rootScope.$on('$routeChangeStart', function() {
    if (!user.isAuthenticated()) {
        $location.path('/login');
    }
  });

see this punker http://plunker.co/edit/SnBuFPXSLMBd8G9R0KXl?p=preview

g00fy
  • 4,717
  • 1
  • 30
  • 46
  • If your `isAuthenticated` function is a promise or has other http overhead, the flickering is just a result of race condition between the user action and route validity. – Ken Sep 30 '13 at 18:12
  • There is also this: http://stackoverflow.com/questions/11972026/delaying-angularjs-route-change-until-model-loaded-to-prevent-flicker – Ken Sep 30 '13 at 18:15

1 Answers1

0

The problem isn't with the way you cancel the location change. It's the ng-animate on the ng-view that causes the flickering. You can still see it when you just switch locations without having the auth validation.

yohairosen
  • 1,665
  • 1
  • 18
  • 26