我是Angular的新手,我想知道我的代码有什么问题,因为浏览器显示了这个错误:错误:[ng:areq] http://errors.angularjs.org/1.4.7/ng/areq?p0=HelloWorldCtrl&p1=not%20a%20function%2C%20got%20undefined 在错误(本机)
y el codigo es este:
<!doctype html>
<html ng-app>
<head>
<title>Angular Practice</title>
</head>
<body>
<h1 ng-controller="HelloWorldCtrl">{{helloMessage}}</h1>
<script src="angular.min.js"></script>
<script type="text/javascript">
function HelloWorldCtrl($scope) {
$scope.helloMessage = "Angular Practice";
}
</script>
</body>
</html>
非常感谢。
答案 0 :(得分:0)
您的HelloWorldCtrl尚未定义。
这是因为您没有将它绑定为带有控制器的角度模块。
ng-controller寻找这样的定义:
<% Arraylist<Products> mypr = sesion.getAttribute("products"); %>
<table class="table table-condensed">
<tr>
<th>Id</th>
<th>Category</th>
<th>Model</th>
<th>Stock</th>
<th>Price</th>
</tr>
<%
for (Products xx : mypr) {
out.println(xx.toString() + "<br>");
}
%>
还需要将该控制器分配给主应用程序,您需要在该应用程序上引用该指令,即
angular.module('HelloWorldCtrl', [])
.controller('HelloWorldCtrl', function($scope){
$scope.helloMessage = "Angular Practice";
});
哪个应指向您创建的模块:
<html ng-app="helloWorldApp">
注意,我包括一个“HelloWorldCtrl”&#39;引用作为该模块定义的第二个参数的项。这告诉angular将该控制器加载为一个资源,然后您可以通过该ng-controller指令进行引用。
编辑:
对我提到添加&#39; HelloWorldCtrl&#39;进行了一些研究。作为上面数组中的项目,并想详细说明为什么我的解决方案与此处的其他答案略有不同。我设置它的方式就是“HelloWorldCtrl&#39;是一个单独的模块。在这种情况下,您需要以我的方式引用它。这告诉app模块它依赖于&#;; HelloWorldCtrl&#39;模块。在下面的答案中,我将控制器直接绑定到应用程序,在这种情况下,这是不必要的。
答案 1 :(得分:0)
使其运作的步骤,
<html>
代码app
。HelloWorldCtrl
,如下所示。检查此Plunker - http://plnkr.co/edit/w8RWm6nr1WgvhX3BbGUE?p=preview
!DOCTYPE html>
<html ng-app="app">
<head>
<script src="http://apps.bdimg.com/libs/angular.js/1.4.0-beta.4/angular.min.js"></script>
</head>
<h1 ng-controller="HelloWorldCtrl">{{helloMessage}}</h1>
<script type="text/javascript">
angular.module('app', []);
angular.module('app').controller('HelloWorldCtrl', [ '$scope', function($scope) {
$scope.helloMessage = "Angular Practice";
}]);
</script>
</html>