【问题标题】:Angular.JS: cannot set default route when no hashAngular.JS:没有哈希时无法设置默认路由
【发布时间】:2013-11-13 03:24:00
【问题描述】:

问题是: 当 url 是 www.some.com 时我无法访问主页,但当它是 www.some.com/#!www.some.com/#!/

时可以

我定义了默认的网络应用程序路由:

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

添加了其他选项:

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

并开启html5模式

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

正如我之前所说的 - 当我通过 ulr 来时,它会起作用,例如 www.some.com/#!,但在 www.some.com 时不起作用。在这种情况下,将调用 .otherwise 选项。

在任何其他情况下,路由都可以正常工作。在我的应用程序中,我得到了一个网址,例如 www.some.com/#!/login、www.some.com/#!/signup

附:服务器端在php5+nginx上工作 附言我使用 Angular 1.2.0 和 ngRoute 模块

【问题讨论】:

  • 我可以尝试用$routeProvider.otherwise({redirectTo: '/'});解决这个问题。它会起作用,但是在这种情况下我应该如何处理 404 错误?

标签: javascript angularjs url-routing


【解决方案1】:

尝试设置 标签。

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

为家定义路线

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

在“/”上使用redirectTo

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

连同.otherwise

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    • 2019-12-05
    相关资源
    最近更新 更多