为什么我的angularJS控制器不起作用?

时间:2014-11-14 20:59:36

标签: angularjs

今天我决定尝试一些AngularJS的技巧!有人能解释一下我的代码有什么问题吗?这个例子显示在我下载的视频中,用于学习Angular。当我尝试它时,它会向我显示错误提示:



<html data-ng-app="">
<head>
<link rel="stylesheet" type="text/css" href="style.css">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<script type="text/javascript" src="angular.min.js"></script>
	<script type="text/javascript" src="angular.min.js"></script>
	<script>
		function Test($scope){
			$scope.persons =
			[{name: 'Me', city:'Saint Pete'}, {name: 'Other', city:'Moscow'}]};
	</script>
<title>
	ANGULAR
</title>

</head>

<body>
	<div data-ng-controller="Test">
		<input type="text" data-ng-model="name">
		<ul>
			<li data-ng-repeat="person in persons | filter: name"> {{person.name}} - {{person.city}} </li>
		</ul>
	</div>
</body>
</html>
&#13;
&#13;
&#13;

这是控制台中的错误提示: 错误:[ng:areq] http://errors.angularjs.org/1.3.2/ng/areq?p0=Test&p1=not%20a%20function%2C%20got%20undefined     在错误(本机)     at file:/// C:/ Program%20Files%20(x86)/EasyPHP-DevServer-14.1VC9/data/localweb/projects/angular/angular.min.js:6:416     在Nb(file:/// C:/ Program%20Files%20(x86)/EasyPHP-DevServer-14.1VC9/data/localweb/projects/angular/angular.min.js:19:417)     at ob(file:/// C:/ Program%20Files%20(x86)/EasyPHP-DevServer-14.1VC9/data/localweb/projects/angular/angular.min.js:20:1)     at file:/// C:/ Program%20Files%20(x86)/EasyPHP-DevServer-14.1VC9/data/localweb/projects/angular/angular.min.js:75:177     在file:/// C:/ Program%20Files%20(x86)/EasyPHP-DevServer-14.1VC9/data/localweb/projects/angular/angular.min.js:57:112     at r(file:/// C:/ Program%20Files%20(x86)/EasyPHP-DevServer-14.1VC9/data/localweb/projects/angular/angular.min.js:7:408)     at I(file:/// C:/ Program%20Files%20(x86)/EasyPHP-DevServer-14.1VC9/data/localweb/projects/angular/angular.min.js:56:496)     at g(file:/// C:/ Program%20Files%20(x86)/EasyPHP-DevServer-14.1VC9/data/localweb/projects/angular/angular.min.js:51:299)     at g(file:/// C:/ Program%20Files%20(x86)/EasyPHP-DevServer-14.1VC9/data/localweb/projects/angular/angular.min.js:51:316)

2 个答案:

答案 0 :(得分:1)

ng-app标签应该有一个名称,您的测试定义应如下所示: ng-app="mayApp" angular.module ("myapp",[]) //definition of the angular module 您的测试应定义为控制器,如下所示:
      angular.module ("myapp").controller ("Test", function Test (){            //test function should go here.       });

答案 1 :(得分:0)

您尚未在页面中添加AngularJS。只需在头部添加以下行。

<head>
<script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
</head>

你也可以下载angularjs文件并给出它的路径。