Angular redirect to home on refresh

10,051

I havn't used the ui-router but in ng-route we can do it like this

  • create a controller on top of your ng-view div
<body ng-controller="topController">
<div ng-view></div>
</body>
  • Define your routes normally but in topController just write this line

$location.path("/")

assuming that "/" is your main page where you want to redirect after refresh, don't forget to define $location

Share:
10,051
Augie Gardner
Author by

Augie Gardner

Not your average coder. www.opensourceaugie.com/css3d/css3d.php

Updated on June 20, 2022

Comments

  • Augie Gardner
    Augie Gardner about 2 years

    What's the best way to direct a user to the home page if they happen to refresh from any other route (or state if you're using ui-router, as I am).

    For example - I want them to begin on the inventory page. If they want to make edits, they travel to the edits page, but if they refresh that page, they go straight to the inventory route again.

  • Augie Gardner
    Augie Gardner about 10 years
    "otherwise" is for invalid routes. If a user heads over to the "edits" page, and hits refresh, they are technically on a valid route, so the otherwise will not be processed, and will not work.
  • Augie Gardner
    Augie Gardner about 10 years
    I feel a little silly to have forgotten the outer controller of my ui-view. Thanks