ng-app和ngRoute无法正常工作

时间:2017-07-18 21:36:44

标签: angularjs angularjs-ng-route

我有以下script.js代码

var app = angular
        .module("Demo", ["ngRoute"])
        .config(function ($routeProvider) {
            $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;
                                    })
         })

以下是html:

      <body ng-app="Demo">
<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>

路由不适用于上述href链接。我的部分页面在script.js文件中使用$ Scope的属性。但是点击链接后,home.html永远不会被加载。请帮助解释为什么ng-app,ng-view和ngRoute对我不起作用。

2 个答案:

答案 0 :(得分:0)

删除<a>标记中的哈希值。

答案 1 :(得分:0)

它现在有效,因为我更新了代码如下:     var app = angular.module('Demo',['ngRoute'])       .config(function($ routeProvider,$ locationProvider){           $ locationProvider.hashPrefix(''); //这有助于修复角度为1.6.1 http://stackoverflow.com/questions/41211875/angularjs-1-6-0-latest-now-routes-not-working的错误,路由链接不起作用,但这个$ locationProvider变量修改了它。           $ routeProvider           .when('/ home',{               templateUrl:'Templates / home.html',               控制器:'homeController'           })           .when('/ courses',{               templateUrl:'Templates / courses.html',               控制器:'coursesController'           })           .when('/ students',{               templateUrl:'Templates / students.html',               控制器:'studentsController'           });       })