角度路由的工作流程是什么?

时间:2017-02-16 17:00:21

标签: angularjs angularjs-routing

我正在使用angula rjs例程,一切正常,但路由给我一个奇怪的问题。当我点击index.html页面上的链接时,网址如下所示

http://localhost:0000/Index.html#%2Fhome

它应该是什么样的:

http://localhost:0000/Index.html#/home

当我从" / home"更改路由功能时只有" /"我的应用程序运行良好,我不明白我的错误在哪里,以下是我的路由文件:

var app = angular
            .module("Demo", ["ngRoute"])
            .config(function ($routeProvider, $locationProvider) {
                $locationProvider.html5Mode(true);
                $routeProvider
                    .when("/home", {
                        templateUrl: "Templates/home.html",
                        controller: "homeController"
                    })
                    .when("/courses", {
                        templateUrl: "Templates/courses.html",
                        controller: "coursesController"
                    })
                    .when("/students", {
                        templateUrl: "Templates/students.html",
                        controller: "studentsController"
                    })
            })
            .controller("homeController", function ($scope) {
                $scope.message = "Home Page";
            })
            .controller("coursesController", function ($scope) {
                $scope.courses = ["C#", "VB.NET", "ASP.NET", "SQL Server"];
            })
             .controller("studentsController", function ($scope, $http) {
                 $http.get("StudentService.asmx/GetAllStudents")
                                        .then(function (response) {
                                            $scope.students = response.data;
                                        })
             })

以下是我的index.html:

<!DOCTYPE html>
<html ng-app="Demo">
<head>
    <title></title>
    <script src="scripts/angular.min.js"></script>
    <script src="scripts/angular-route.min.js"></script>
    <link href="Styles.css" rel="stylesheet" />
    <script src="scripts/Script.js"></script>
    <base href="/"/>
    <meta charset="utf-8" />
</head>
<body>
    <table style="font-family: Arial">
        <tr>
            <td colspan="2" class="header">
                <h1>
                    WebSite Header
                </h1>
            </td>
        </tr>
        <tr>
            <td class="leftMenu">
                <a href="#/home">Home</a>
                <a href="#/courses">Courses</a>
                <a href="#/students">Students</a>
            </td>
            <td class="mainContent">
                <ng-view></ng-view>
            </td>
        </tr>
        <tr>
            <td colspan="2" class="footer">
                <b>Website Footer</b>
            </td>
        </tr>
    </table>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

所以,我按照给定的评论并更新了index.html,如下所示,这对我有用。谢谢!

<!DOCTYPE html>
<html ng-app="Demo">
<head>
    <title></title>
    <script src="scripts/angular.min.js"></script>
    <script src="scripts/angular-route.min.js"></script>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <link href="Styles.css" rel="stylesheet" />
    <script src="scripts/Script.js"></script>
    <base href="/"/>
    <meta charset="utf-8" />
</head>
<body>
    <table style="font-family: Arial">
        <tr>
            <td colspan="2" class="header">
                <h1>
                    WebSite Header
                </h1>
            </td>
        </tr>
        <tr>
            <td class="leftMenu">
                <a href="/home">Home</a>
                <a href="/courses">Courses</a>
                <a href="/students">Students</a>
            </td>
            <td class="mainContent">
                <ng-view></ng-view>
            </td>
        </tr>
        <tr>
            <td colspan="2" class="footer">
                <b>Website Footer</b>
            </td>
        </tr>
    </table>
</body>
</html>