简体   繁体   中英

AngularJS: How to change the path after refresh the page

What I want: if the user reloads the page with his redirect to another page.

(Only when updating the page)

Example: I'm on main/bla and reload the page and I'm redirected to the main/

window.onbeforeunload = function(e) {
 $state.go('main');
};

Does not work for me.

Check once if you are using proper injector in js controller or is there any other error on console. Check for:

  1. you have given proper state name $state.go('main'),it should defined in routing.
  2. Should use injector $state in controller.

There is a lot happening when you want to change states.

For one, it could trigger extra network activities such as resource download. There isn't enough time for angular to change states, change URL and go through the lifecycle of navigating routes.

some options:

  • Do the redirect after the refresh happens (save something in localstorage, cookies, ..)
  • Redirect manually, update document.location or similar

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM