简体   繁体   English

ngRoute返回404找不到错误

[英]ngRoute return 404 Not Found error

I'm building a simple personal contacts management app using AngularJS. 我正在使用AngularJS构建一个简单的个人联系人管理应用程序。 I put all my files inside htdocs/angular-contacts/ folder of my Mac machine. 我将所有文件放入Mac机器的htdocs/angular-contacts/文件夹中。

在此处输入图片说明

index.html 的index.html

<!DOCTYPE html>
<html ng-app='ContactsApp'>
    <head>
        <title>Contacts</title>
        <base href='/'></base>
    </head>
    <body>
        <h1>Contacts</h1>
        <div ng-view=""></div>
        <script src='/angular-contacts/jquery-2.1.3.min.js'></script>
        <script src='/angular-contacts/angular.min.js'></script>
        <script src='/angular-contacts/angular-route.min.js'></script>
        <script src='/angular-contacts/angular-route.min.js.map'></script>
        <script src='/angular-contacts/app.js'></script>
        <script src='/angular-contacts/controller.js'></script>
    </body>
</html>

app.js app.js

angular.module('ContactsApp', ['ngRoute']);
    .config(function($routeProvider,$locationProvider){
        $routeProvider
            .when('/contacts',{
                controller: 'ListController',
                templateUrl: 'list.html'
            });
        $locationProvider.html5Mode(true);
    });

controller.js controller.js

angular.module('ContactsApp')
    .controller('ListController',function($scope){
        $scope.contacts = [];
    })

list.html list.html

<p>List views...</p>

Why I go to http://localhost:8888/angular-contacts/contacts , I got 404 Not Found error. 为什么我去http://localhost:8888/angular-contacts/contacts ,却收到404 Not Found错误。

How to fix this problem? 如何解决这个问题? How to load that list.html ? 如何加载list.html

Note : 注意事项

  1. I'm using MAMP (Apache). 我正在使用MAMP(Apache)。
  2. AngularJS: v1.3.14 AngularJS:v1.3.14
  3. Angular Route: v1.3.14 角线:v1.3.14

That's because 那是因为

$routeProvider
    .when('/contacts',{
        controller: 'ListController',
        templateUrl: 'list.html'
    });

expects the http://localhost:8888/contacts location, not the http://localhost:8000/angular-contacts/contats location. 期望使用http://localhost:8888/contacts位置,而不是http://localhost:8000/angular-contacts/contats位置。

You should add the prefix in your route declaration, or even better, conifgure the virtual host , so you'll be able to use a real domain, rather than the localhost's sub-catalogue. 您应该在路由声明中添加前缀,或者甚至更好地配置虚拟主机 ,这样您就可以使用真实域,而不是本地主机的子目录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM