对AngularJS来说是全新的。因此,在我开始在项目中开发新应用程序之前,我正在尝试示例应用程序的流程。以下是我的尝试。
的index.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html ng-app="app">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script src="lib/angular.min.js"></script>
<script src="lib/angular-route.js"></script>
<script type="text/javascript" src = "js/app.js"></script>
</head>
<body>
<h1>Student Registration!</h1>
<div ng-view></div>
</body>
</html>
registerStudent.html
<table>
<tr>
<td><input type="button" value="Save" ng-click="save()"></td>
<td>{{message}}</td>
</tr>
</table>
app.js
var app = angular.module("app", ['ngRoute', 'app.services', 'app.controllers']);
/**routing*/
app.config(['$routeProvider', function($routeProvider){
$routeProvider.when('/editStudent', {
templateUrl:'html/editStudent.html',
controller:'studentReg'
});
$routeProvider.when('/viewStudent', {
templateUrl:'html/viewStudent.html',
controller:'studentReg'
});
$routeProvider.when('/registerStudent', {
templateUrl:'html/registerStudent.html',
controller:'studentReg'
});
$routeProvider.otherwise({
redirectTo: '/registerStudent'
});
}]);
services.js
var app = angular.module('app.services', []);
app.service('restService', function(){
this.save=function(){
return 'saved';
};
});
controller.js
var app = angular.module('app.controllers', []);
app.controller('studentReg', function($scope, restService){
$scope.result=restService.save();
$scope.save=function(){
console.log($scope.result);
$scope.message=$scope.result;
};
});
当我尝试运行该应用程序时,出现以下错误。
Uncaught Error: [$injector:modulerr] http://errors.angularjs.org/1.5.0-rc.1/$injector/modulerr?p0=app&p1=Error%3…2Flocalhost%3A8080%2FEnhancedStudentform%2Flib%2Fangular.min.js%3A20%3A421)
答案 0 :(得分:2)
您忘记加载您的controller.js和services.js,这是我的猜测。在index.html中包含app.js后尝试此操作:
<script type="text/javascript" src = "js/controller.js"></script>
<script type="text/javascript" src = "js/services.js"></script>
答案 1 :(得分:1)
尝试在索引html中包含所有js文件,如:
<script type="text/javascript" src = "js/app.js"></script>
<script type="text/javascript" src = "js/controller.js"></script>
<script type="text/javascript" src = "js/services.js"></script>