角度实例化模块错误

时间:2016-06-25 19:26:07

标签: javascript html angularjs

请帮助识别,以下角度Hello World有什么问题!第一个计划。

<html>
	<head>
		<script type="text/javascript" language="javascript" src="Scripts/angular.js" />
		<script type="text/javascript" language="javascript" src="Scripts/angular-resource.js" />
		<script type="text/javascript" language="javascript" src="Scripts/angular-route.js" />
		<script type="text/javascript" language="javascript" src="Scripts/angular-cookies.js" />
		<script type="text/javascript" language="javascript" src="Scripts/angular-sanitize.js" />
      
		<script type="text/javascript" language="javascript">
			function Student($scope){
				$scope.Name= 'Akshay';
				$scope.City= 'Mumbai';				
			};
			
			var app = angular.module("myApp");
			app.controller("Student", Student)
		</script>
	</head>
	<body ng-app="myApp">
		<form>
		<div id="dvStudent" ng-controller="Student">
			Name: <input type="text" name="txtStudentName" id="txtStudentName" ng-model="Name" />
			City: <input type="text" name="txtStudentCity" id="txtStudentCity" ng-model="City" />
			<input type="button" name="btnSave" id="btnStudentSave" value="Save" />&nbsp;<input type="reset" name="btnReset" id="btnStudentReset" value="Reset" />
			<br />			
			You have inputed Name: {{Name}} & City: {{City}}
		</div>
		</form>
	</body>
</html>

这是我得到的错误

angular.js:68 Uncaught Error: [$injector:modulerr] Failed to instantiate module myApp due to:
Error: [$injector:nomod] Module 'myApp' 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.7/$injector/nomod?p0=myApp

2 个答案:

答案 0 :(得分:0)

您没有向我们提供完整的错误,但很可能问题是您错过了angular.module()的第二个参数:

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

另外,仅供参考,您不应对<script>元素使用自闭标记。您应该明确包含结束标记。此外,不推荐使用language属性,当脚本为JavaScript时,不需要type属性:

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

答案 1 :(得分:0)

由于您没有提供完整错误,我发现了您的错误并为您提供了初始化index.html的步骤

第1步制作 <head> <script type="text/javascript" language="javascript" src="lib/angular.js" /> <script type="text/javascript" language="javascript" src="js/app.js" /> </head> <body ng-app="myApp"> <form> <div id="dvStudent" ng-controller="Student"> <label>Name:</label> <input type="text" name="txtStudentName" id="txtStudentName" ng-model="Name" /> <label>City:</label> <input type="text" name="txtStudentCity" id="txtStudentCity" ng-model="City" /> <input type="button" name="btnSave" id="btnStudentSave" value="Save" /> <input type="reset" name="btnReset" id="btnStudentReset" value="Reset" /> <br /> <p>You have inputed Name: {{Name}} & City: {{City}} </p> </div> </form> </body> </html>

lib

第2步将两个文件夹jsapp.js放在lib中,将var app = angular.module("myApp",[]); app.controller("Student", funtion($scope){ $scope.Name= 'Akshay'; $scope.City: 'Mumbai'; }); 放在js文件夹中

app.js

package.json