0

this has been posted many times but I'm still not having luck with this:

$rootScope.$on("$stateChangeError", function(event, toState, toParams, fromState, fromParams, error) {
  if (error && !error.authenticated) {
    $location.path('/login');
    console.log('new path: ' + $location.path());
  }
});

My app will in fact not go to /login, yet the console.log will return a value of /login. I've tried the $apply trick without luck mentioned in other threads but this is a very simple app at this point, no third party integrations where angular needs some additional help via $apply. Is there a variant to $location.path() needed when used within the context of $rootScope, or listening for $stateChangeError?

I'm sure this is something simple and I'm just not seeing it.

2
  • Can you post a plunker? Commented Oct 25, 2014 at 7:12
  • Not enough context to speculate about why it's not working. You would need to provide a way for people to reproduce it, please see stackoverflow.com/help/mcve. Emphasis on minimal, often you'll solve the problem yourself while trying to produce a minimal example. Commented Oct 25, 2014 at 7:16

1 Answer 1

1

I failed to mention I'm using ui.router and so switching to $state.go('login') seemed to make it work. Next time I will make sure to post a plunker to get something reproducible and perhaps solve my own problem :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.