我已閱讀與同一問題相關的帖子並交叉驗證了我的代碼,但問題仍然存在。
以下是我的index.html文件
<!DOCTYPE html>
<html ng-app="myModule">
<head>
<title></title>
<script src="Scripts/angular.js"></script>
<script src="Scripts/angular-route.js"></script>
<link href="Scripts/styles.css" rel="stylesheet" />
<meta charset="utf-8" />
</head>
<body>
<table style="font-family:Arial">
<tr>
<td class="header" colspan="2" style="text-align:center">Website Header</td>
</tr>
<tr>
<td class="leftMenu">
<a href="#/home">Home</a><br/>
<a href="#/courses">Courses</a><br />
<a href="#/students">Students</a><br />
</td>
<td class="mainContent">
<div><ng-view></ng-view></div>
</td>
</tr>
</table>
</body>
</html>
的script.js文件
/// <reference path="angular-route.js" />
/// <reference path="angular.js" />
var myModule = angular.module("myModule", ["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 = "HomePage";
})
.controller("coursesController", function ($scope) {
$scope.courses = ["c","c++","c#"];
})
.controller("studentsController", function ($scope) {
$scope.students = [{ name: "Chandu", id: 1, gender: "female" }, { name: "Suma", id: 2, gender: "female" }, { name: "Arin", id: 3, gender: "male" }];
})
})
我已經定義了家,課程和學生的html文件。初始加載後,當我點擊任何鏈接例如。 home,/#/ home正在追加到url,但部分模板未加載。
起初,我也沒有得到任何控制檯錯誤。但現在我收到以下錯誤,即使Mymodule中的名稱在這兩個的script.js正確使用和index.html文件
Uncaught Error: [$injector:modulerr] Failed to instantiate module myModule due to:
Error: [$injector:nomod] Module 'myModule' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.
http://errors.angularjs.org/1.5.9/$injector/nomod?p0=myModule
我非常感謝所有幫助我解決問題,幫我找出我錯了。
P.S:Index.html位於主目錄中,部分模板位於主目錄中的模板文件夾中。我正在使用angular.js和angular-route.js的版本1.5.9。
我已將angular.js腳本路徑添加到html文件 – user3794853
已更新的答案顯示您需要添加的內容 – rlcrews
這是我忽略的愚蠢錯誤。謝謝您的幫助。現在我有一個工作代碼! :-) – user3794853