ngRoute无法处理刷新或未知路由

时间:2016-04-12 13:47:38

标签: javascript angularjs angularjs-ng-route

我遇到了代码问题。

在localhost上刷新页面时:portnumber / home或/ todos或/ contacts会崩溃 - >

带重播不能GET / home或/ todos或/ contacts。

同样的问题,如果我只输入localhost:portnumber / blablabla - >

重播与Can not GET / blablabla

但如果我只输入localhost:portnumber,它会重播localhost:portnumber / home,这很好!

我需要修复刷新问题和未知路径

我的ngRoute代码:

angular.module('contactsApp' , ['ngRoute', 'ngMessages'])
/*
    ROUTE configuration
    when href is... do...
*/
.config(function ($routeProvider, $locationProvider) {
    $routeProvider
        .when('/contacts', {
            controller: 'listCtrl',
            templateUrl: '/views/list.html'
        })
        .when('/contacts/new', {
            controller: 'newCtrl',
            templateUrl: '/views/new.html'
        })
        .when('/contact/:id', {
            controller: 'editCtrl',
            templateUrl: '/views/edit.html'
        })
        .when('/todos', {
            controller: 'todoCtrl',
            templateUrl: '/views/noteslist.html'
        })
        .when('/home', {
            controller: 'homeCtrl',
            templateUrl: '/views/Home.html'
        })
        .otherwise({redirectTo: '/home'});
    $locationProvider.html5Mode(true);
});

我的index.html:

<html ng-app="contactsApp">
<head>

    <base href="/">

    <!-- Custom CSS -->
    <link rel="stylesheet" href="src/css/style.css" type="text/css">
    <link href="src/css/landing-page.css" rel="stylesheet">

    <!-- Bootstrap Core CSS -->
    <link href="src/css/bootstrap.min.css" rel="stylesheet">

</head>
<body>

    <!-- Navigation bar -->
    <nav class="navbar navbar-default navbar-fixed-top topnav" role="navigation">
        <div class="container topnav example2">
            <!-- Brand and toggle get grouped for better mobile display -->
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="/home"><img src="images/logo_noback.jpg" alt="My Office" height="40px" width="auto"/></a>
            </div>
            <!-- Collect the nav links, forms, and other content for toggling -->
            <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                <ul class="nav navbar-nav navbar-right increse-font-17">
                    <li>
                        <a href="home">Home</a>
                    </li>
                    <li>
                        <a href="todos">ToDo</a>
                    </li>
                    <li>
                        <a href="contacts">Contacts</a>
                    </li>
                </ul>
            </div>
        </div>   
    </nav>
    <!-- End Navigation bar -->

    <!-- Changed page contact -->
    <div class="navbar-overlap">
        <div ng-view></div>
    </div> 
    <!-- End Changed page contact -->

    <!-- Sticky footer -->
    <footer class="navbar navbar-default navbar-fixed-bottom topnav footer-backcolor">
        <div class="col-md-12 margin-5">
            <p class="col-md-6 breadcrumb visible-md visible-lg">Copyright &copy; My Office 2016. All Rights Reserved</p>
            <ul class="pull-right breadcrumb visible-md visible-lg">
                    <li><a href="/home">Home</a></li>
                    <li><a href="/todos">Todo</a></li>
                    <li><a href="/contacts">Contacts</a></li>
            </ul>
        </div>
    </footer>
    <!-- End Sticky footer -->

    <script src="src/js/jquery.js"></script>
    <script src="src/js/bootstrap.min.js"></script>
    <script src="lib/angular/angular.min.js"></script>
    <script src="lib/angular/angular-route.min.js"></script>
    <script src="lib/angular/angular-messages.min.js"></script>
    <script src="src/js/app.js"></script>
    <script src="src/js/controller.js"></script>
    <script src="src/js/filters.js"></script> 
    <script src="src/js/derectives.js"></script> 
</body>

1 个答案:

答案 0 :(得分:0)

该错误不在您的角度应用程序中:它是因为您使用html mode进行客户端网址重写。

执行此操作时,您还需要重写URL服务器端,以避免在用户点击F5时提供404,或手动输入页面的URL。

这个问题与NodeJS服务器的问题相同 AngularJS html5mode reloading page