gpt4 book ai didi

javascript - 如何解决angularjs Uncaught Error : [$injector:modulerr]?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:13:19 25 4
gpt4 key购买 nike

我正在使用 AngularJS 的 ng-view 和 ng-template 指令开发单页 Web 应用程序。但是当我运行它时,我的浏览器显示这种类型 [1]: http://i.stack.imgur.com/TPuPo.png错误。

这是我的脚本 block ,包含主模块和路由配置。

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">

<title>AngularJS - Views</title>

<!--AngularJs Library-->
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular-route.min.js"></script>
</head>
<body>
<h2 style="text-align: center;">Simple Application using AngularJS - Views</h2><hr><br>

<!--Start AngularJS App-->
<div ng-app="myApp">

<p><a href="#addStudent">Add Student</a></p>
<p><a href="#viewStudent">View Students</a></p>
<div ng-view></div>

<!--Script for Add Student-->
<script type="text/ng-template" id="addStudent.htm">
<h2>This is the view of Add Student</h2>
{{message}}
</script>

<!--Script for View Students-->
<script type="text/ng-template" id="viewStudent.htm">
<h2>This is the view of all students</h2>
{{message}}
</script>

</div>
<!--End AngularJS App-->

<!--App Necessary Scripts-->
<script>

var myApp = angular.module("myApp", ['ngRoute']);

myApp.config(['routeProvider',function ($routeProvider) {
$routeProvider.

when('/addStudent', {
templateUrl: 'addStudent.htm',
controller: 'AddStudentController'
}).

when('/viewStudent', {
templateUrl: 'viewStudent.htm',
controller: 'ViewStudentController'
}).

otherwise({

redirectTo: '/addStudent'

});
}]);

myApp.controller('AddStudentController', function ($scope) {

$scope.message = "This page will be used to display add student form!!";

});

myApp.controller('ViewStudentController', function($scope){

$scope.message = "This page will be used to display all students!!";

});

</script>
</body>
</html>

我该怎么做才能修复它?

最佳答案

这是您的工作代码。还有一个CodePen的例子,我重新组织了代码,让它更舒服,请大家复习、学习和比较,以供学习之用。 http://codepen.io/alex06/pen/rrzRbE?editors=1010

<html ng-app="mainApp">
<head>
<title>Angular JS Views</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular-route.min.js"></script>
</head>

<body>
<h2>AngularJS Sample Application</h2>
<div>
<p><a href = "#addStudent">Add Student</a></p>
<p><a href = "#viewStudents">View Students</a></p>
<div ng-view></div>
</div>
<script type = "text/ng-template" id = "addStudent.htm">
<h2> Add Student </h2>
{{message}}
</script>
<script type = "text/ng-template" id = "viewStudents.htm">
<h2> View Students </h2>
{{message}}
</script>
<script>
(function(){
'use strict'
angular
.module('mainApp', ['ngRoute'])
.config(['$routeProvider', function($routeProvider) {
$routeProvider
.when('/addStudent', {
templateUrl: 'addStudent.htm',
controller: 'AddStudentController'
})
.when('/viewStudents', {
templateUrl: 'viewStudents.htm',
controller: 'ViewStudentsController'
})
.otherwise({
redirectTo: '/addStudent'
});
}])
.controller('AddStudentController', function($scope) {
$scope.message = "This page will be used to display add student form";
})
.controller('ViewStudentsController', function($scope) {
$scope.message = "This page will be used to display all the students";
});
})();
</script>
</body>
</html>

关于javascript - 如何解决angularjs Uncaught Error : [$injector:modulerr]?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39779371/

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