在简单的例子AngularJS没有定义控制器错误 [英] Controller not defined error in simple AngularJS example
问题描述
我们正在做的淘汰赛和角之间的比较,以帮助做出哪些用于绑定的决心。由于我没有在任何一个当前开发者,我只想把一个简单的单html页面(index.html的),而多个视图,这样做比较。要做到这一点,我就看到一个淘汰赛Hello World示例一个简单的jsfiddle,从网页复制的源代码,在一个index.html贴吧,改剧本的参考路径,并担任它。没有问题,我可以开始使用淘汰赛应用程序在一个单一的文件,开始测试。
试图做同样的事情角度和我不断收到错误,使我不能确定是什么问题。这里是例子。谁能告诉我,我做错了什么?该的jsfiddle的例子是: http://jsfiddle.net/Hp4W7/637/
下面是code。误差的下方。
<!DOCTYPE HTML>
< HTML和GT;
< HEAD>
< META HTTP-EQUIV =内容类型内容=text / html的;字符集= UTF-8>
<标题>滚动+ limitTo - 的jsfiddle演示< /标题> <脚本类型=文/ JavaScript的'SRC ='脚本/ angular.js'>< / SCRIPT> <脚本类型=文/ JavaScript的'> //<![CDATA [ VAR应用= angular.module('应用',[]); 功能Ctrl($范围){
$ scope.tasks = {[ID:1,'名':'TEST1'},{ID:2,'名':'测试2'},{ID:3,'名':'TEST3'}] $ scope.removeTask =功能(TASKID){
警报(任务ID为+任务id);
};
}
//]]> < / SCRIPT>
< /头>
<机身NG-应用=应用程序>
< DIV CLASS =测试NG-控制器=CTRL>
< DIV NG重复=任务,任务>
<按钮NG点击=removeTask(task.id);>删除< /按钮>
< / DIV>
< / DIV>< /身体GT;
< / HTML>
这是错误消息。
错误:[NG:AREQ]参数按Ctrl不是一个函数,得到了不确定
http://errors.angularjs.org/1.3.0-rc.5/ng/areq?p0=Ctrl&p1=not%20a%20function%2C%20got%20undefined
在assertArg(HTTP://myhost/angulartest/scripts/angular.js:1609:5)
在assertArgFn(HTTP://myhost/angulartest/scripts/angular.js:1619:3)
在匿名函数(HTTP://myhost/angulartest/scripts/angular.js:8074:9)
在匿名函数(HTTP://myhost/angulartest/scripts/angular.js:7251:13)
在的forEach(HTTP://myhost/angulartest/scripts/angular.js:343:11)
在nodeLinkFn(HTTP://myhost/angulartest/scripts/angular.js:7238:11)
在compositeLinkFn(HTTP://myhost/angulartest/scripts/angular.js:6746:13)
在compositeLinkFn(HTTP://myhost/angulartest/scripts/angular.js:6749:13)
在publicLinkFn(HTTP://myhost/angulartest/scripts/angular.js:6625:30)
在匿名函数(HTTP://myhost/angulartest/scripts/angular.js:1488:11)
块引用>解决方案这问题与他们AngularJS 1.3所做的更改。
您可以在此链接的https://github.com/angular/angular.js/commit/3f2232b5a181512fac23775b1df4a6ebda67d018.
的想法是,让他们默认禁用该选项全局控制器是很不礼貌的。如果要启用这种能力,你必须设置
$ controllerProvider.allowGlobals();
。否则,你应该设置你的控制器象下面这样:app.controller(Ctrl键,[$范围功能($范围){
// code
}]);We are doing a comparison between knockout and angular to help make a determination about which to use for binding. Since I'm not a current developer in either, I just wanted to put together a simple single html page (index.html) without multiple views to do this comparison. To make this happen, I viewed a simple jsfiddle regarding a knockout hello world example, copied the source from the page, pasted it in an index.html, changed the script reference paths, and served it up. No problems I was able to start using a knockout app in a single file to start testing.
Tried to do the same thing with Angular and I keep getting errors that make me unsure what the problem is. Here is the example. Can anyone tell me what I'm doing wrong? The jsfiddle example is: http://jsfiddle.net/Hp4W7/637/
Here is the code. The error is below.
<!DOCTYPE html> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Scroll + limitTo - jsFiddle demo</title> <script type='text/javascript' src='scripts/angular.js'></script> <script type='text/javascript'>//<![CDATA[ var app = angular.module('app', []); function Ctrl($scope) { $scope.tasks = [{id:1,'name':'test1'}, {id:2,'name':'test2'}, {id:3,'name':'test3'}]; $scope.removeTask = function(taskId){ alert("Task Id is "+taskId); }; } //]]> </script> </head> <body ng-app="app"> <div class="test" ng-controller="Ctrl"> <div ng-repeat="task in tasks"> <button ng-click="removeTask(task.id);">remove</button> </div> </div> </body> </html>
And here is the error message.
Error: [ng:areq] Argument 'Ctrl' is not a function, got undefined http://errors.angularjs.org/1.3.0-rc.5/ng/areq?p0=Ctrl&p1=not%20a%20function%2C%20got%20undefined at assertArg (http://myhost/angulartest/scripts/angular.js:1609:5) at assertArgFn (http://myhost/angulartest/scripts/angular.js:1619:3) at Anonymous function (http://myhost/angulartest/scripts/angular.js:8074:9) at Anonymous function (http://myhost/angulartest/scripts/angular.js:7251:13) at forEach (http://myhost/angulartest/scripts/angular.js:343:11) at nodeLinkFn (http://myhost/angulartest/scripts/angular.js:7238:11) at compositeLinkFn (http://myhost/angulartest/scripts/angular.js:6746:13) at compositeLinkFn (http://myhost/angulartest/scripts/angular.js:6749:13) at publicLinkFn (http://myhost/angulartest/scripts/angular.js:6625:30) at Anonymous function (http://myhost/angulartest/scripts/angular.js:1488:11)
解决方案This issue is related to a change they made in 1.3 of AngularJS.
You can read the change here at this link https://github.com/angular/angular.js/commit/3f2232b5a181512fac23775b1df4a6ebda67d018.
The idea is that global controllers were very bad form so they disabled that option by default. If you want to enable that ability you have to set
$controllerProvider.allowGlobals();
. Otherwise you should set up your controller like below:app.controller("Ctrl", ["$scope", function($scope) { //Code }]);
这篇关于在简单的例子AngularJS没有定义控制器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!