Sergey Panfilov Sergey Panfilov - 3 months ago 19
AngularJS Question

Angular.JS: cannot set default route when no hash

The problem is:
I cannot reach home page when url is www.some.com but can when it's

www.some.com/#!
or
www.some.com/#!/


I was define default web app route:

$routeProvider.when('', {templateUrl: pathToIncs + 'home.html', controller: 'homeController'});
$routeProvider.when('/', {templateUrl: pathToIncs + 'home.html', controller: 'homeController'});


Added otherwise option:

$routeProvider.otherwise({redirectTo: '/404'});


And turn of html5 mode

$locationProvider.html5Mode(false);
$locationProvider.hashPrefix('!');


As i said befor - it works when I'm came by ulr like www.some.com/#! but not when www.some.com. In this case .otherwise option will be called.

In any other case routing works well. In the my app I got an urls like a www.some.com/#!/login, www.some.com/#!/signup

P.S. Server side works on php5+nginx
P.P.S. I'm use Angular 1.2.0 with ngRoute module

Answer

Try setting the < base > tag.

<head>
  <base href="/">
</head>

Have a route defined for home

.when('/home', {
        templateUrl: pathToIncs + 'home.html', 
        controller: 'homeController'
 })

using redirectTo on the "/"

.when('/', {
    redirectTo: '/home'
})

along with the .otherwise

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