Angular.JS: cannot set default route when no hash

岁酱吖の 提交于 2020-01-05 03:18:48

问题


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


回答1:


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'
});


来源:https://stackoverflow.com/questions/19944657/angular-js-cannot-set-default-route-when-no-hash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!