小编Die*_*ker的帖子

使用AngularJS HTML5模式重新加载页面会产生错误的GET请求

我想为我的应用启用HTML5模式.我已经把下面的代码的配置,如图所示在这里:

return app.config(['$routeProvider','$locationProvider', function($routeProvider,$locationProvider) {

    $locationProvider.html5Mode(true);
    $locationProvider.hashPrefix = '!';

    $routeProvider.when('/', {
        templateUrl: '/views/index.html',
        controller: 'indexCtrl'
    });
    $routeProvider.when('/about',{
        templateUrl: '/views/about.html',
        controller: 'AboutCtrl'
    });
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,我使用了$locationProvider.html5mode和我改变了我所有的链接ng-href来排除/#/.

问题

目前,我可以去localhost:9000/看看索引页面并导航到其他页面localhost:9000/about.

但是,刷新localhost:9000/about页面时会出现问题.我得到以下输出:Cannot GET /about

如果我看一下网络电话:

Request URL:localhost:9000/about
Request Method:GET
Run Code Online (Sandbox Code Playgroud)

如果我先去localhost:9000/,然后点击导航到的按钮,/about我会得到:

Request URL:http://localhost:9000/views/about.html
Run Code Online (Sandbox Code Playgroud)

这使页面完美呈现.

如何在刷新时启用角度来获取正确的页面?

html5 angularjs angular-routing

193
推荐指数
9
解决办法
14万
查看次数

标签 统计

angular-routing ×1

angularjs ×1

html5 ×1