Angular redirect to home on refresh

Augie Gardner picture Augie Gardner · May 16, 2014 · Viewed 10k times · Source

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.

Answer

cutedevil086 picture cutedevil086 · May 16, 2014

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