userMod2
userMod2

Reputation: 9000

Angular - Redirect if page not specified in routes

In my Angular app I'm using routes so I've specified my various pages like:

$routeProvider.when('/'...

.when('/login'...

.when('/story'...

However if a user types something not in that list it shows (as expected) a blank ng-view. I'd rather they are redirected to the home page.

How do i do that - "if a page (when) is not specified in routes direct to homepage"?

Thanks.

Upvotes: 1

Views: 51

Answers (2)

d3l33t
d3l33t

Reputation: 890

.otherwise({
    redirectTo: '/home'
});

https://docs.angularjs.org/api/ngRoute/provider/$routeProvider

Upvotes: 1

reptilicus
reptilicus

Reputation: 10407

Use the 'otherwise' option in the router:

 .otherwise({redirectTo:'/'})

Upvotes: 2

Related Questions