gpt4 book ai didi

javascript - $routeProvider 未加载部分模板

转载 作者:行者123 更新时间:2023-11-28 15:00:23 25 4
gpt4 key购买 nike

我已阅读与同一问题相关的帖子并交叉验证了我的代码,但问题仍然存在。

以下是我的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" }];
})
})

我已经定义了主页、类(class)和学生 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 在主目录中,部分模板在主目录的 templates 文件夹中。我使用的是 1.5.9 版本的 Angular.js 和 Angular-route.js

最佳答案

您还没有将模块 js 文件添加到您的 html 页面中。您只需加载需要在 Angular 库之后添加 script.js 文件的 Angular 库。该错误表明找不到模块,因为尚未加载。

在标题中您需要添加以下内容

<script src="Scripts/angular.js"></script>
<script src="Scripts/angular-route.js"></script>

<script src="Scripts/script.js"></script><!-- loading your module -->

<link href="Scripts/styles.css" rel="stylesheet" />
<meta charset="utf-8" />

关于javascript - $routeProvider 未加载部分模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41199380/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com