Javascript Angularjs:错误:[ng:areq] 参数“HomeController”不是函数,未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25895235/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Angularjs: Error: [ng:areq] Argument 'HomeController' is not a function, got undefined
提问by Cuong.Ho
This is my demo using angularjs, for creating a service file, and adding service to a controller.
这是我使用 angularjs 创建服务文件并将服务添加到控制器的演示。
I have two problems with my demo:
我的演示有两个问题:
- One is when I put
<script src="HomeController.js">before<script src="MyService.js">I get this error,
- 一个是当我
<script src="HomeController.js">在<script src="MyService.js">收到此错误之前放置时,
Error: [ng:areq] Argument 'HomeController' is not a function, got undefined
错误:[ng:areq] 参数“HomeController”不是函数,未定义
- The other is when I put
<script src="MyService.js">before<script src="HomeController.js">I get the following error,
- 另一个是当我
<script src="MyService.js">在<script src="HomeController.js">收到以下错误之前放置时,
Error: [$injector:unpr] Unknown provider: MyServiceProvider <- MyService
错误:[$injector:unpr] 未知提供者:MyServiceProvider <- MyService
My source:
我的来源:
File Index.html:
文件Index.html:
<!DOCTYPE html>
<html >
<head lang="en">…</head>
<body ng-app="myApp">
…
<div ng-controller="HomeController">
<div ng-repeat="item in hello">{{item.id + item.name}}</div>
</div>
<script src="Scripts/angular.js"></script>
<script src="Scripts/angular-route.js"></script>
<!-- App libs -->
<script src="app/app.js"></script>
<script src="app/services/MyService.js"></script>
<script src="app/controllers/HomeController.js"></script>
</body>
</html>
File HomeController.js:
文件HomeController.js:
(function(angular){
'use strict';
var myApp = angular.module('myApp',[]);
myApp.controller('HomeController',function($scope,MyService){
$scope.hello=[];
$scope.hello = MyService.getHello();
});
})(window.angular);
File MyService.js:
文件MyService.js:
(function(angular){
'use strict';
var myApp = angular.module('myApp',[]);
myApp.service('MyService', function () {
var hello =[ {id:1,name:'cuong'},
{id:2,name:'nguyen'}];
this.getHello = function(){
return hello;
};
});
})(window.angular);
回答by bmleite
This creates a new module/app:
这将创建一个新模块/应用程序:
var myApp = angular.module('myApp',[]);
While this accesses an already created module (notice the omission of the second argument):
虽然这访问了一个已经创建的模块(注意省略了第二个参数):
var myApp = angular.module('myApp');
Since you use the first approach on both scripts you are basically overriding the module you previously created.
由于您在两个脚本上都使用了第一种方法,因此您基本上是在覆盖您之前创建的模块。
On the second script being loaded, use var myApp = angular.module('myApp');.
在加载的第二个脚本上,使用var myApp = angular.module('myApp');.
回答by Jorge Casariego
I experienced this error once. My problem was that I wasn't adding the FILE_NAME_WHERE_IS_MY_FUNCTION.js
我曾经遇到过这个错误。我的问题是我没有添加FILE_NAME_WHERE_IS_MY_FUNCTION.js
so my file.htmlnever found where my function was
所以我的file.html从未找到我的函数在哪里
Once I add the "file.js" I resolved the problem
添加“file.js”后,我解决了问题
<html ng-app='myApp'>
<body ng-controller='TextController'>
....
....
....
<script src="../file.js"></script>
</body>
</html>
回答by AA11oAKas
Also ensure that your controllers are defined within script tags toward the bottom of your index.html just before the closing tag for body.
还要确保您的控制器是在 index.html 底部的 script 标签中定义的,就在 body 的结束标签之前。
<!-- build:js({.tmp,app}) scripts/scripts.js -->
<script src="scripts/app.js"></script>
<script src="scripts/controllers/main.js"></script>
<script src="scripts/controllers/Administration.js"></script>
<script src="scripts/controllers/Leaderboard.js"></script>
<script src="scripts/controllers/Login.js"></script>
<script src="scripts/controllers/registration.js"></script>
provided everything is spelled "correctly" (the same) on your specific.html, specific.js and app.js pages this should resolve your issue.
如果您的 specific.html、specific.js 和 app.js 页面上的所有内容都拼写为“正确”(相同),这应该可以解决您的问题。
回答by jprism
Happened to me few times whenever I miss "," between list of injections and function
每当我错过注射列表和功能之间的“,”时,我就遇到过几次
app.controller('commonCtrl', ['$scope', '$filter',function($scope,$filter) {
}]);
回答by Kuldeep Dangi
I also experienced this error but in my case it was because of controller naming convention. I declared controller: "QuestionController"in .statebut in controller definition I declared it like
我也遇到了这个错误,但就我而言,这是因为控制器命名约定。我声明controller: "QuestionController"了.state但是在控制器定义中我声明了它
yiiExamApp.controller('questionController' ...
but it should be
但应该是
yiiExamApp.controller('QuestionController' ...
hope that helps to people facing this error because of this stupid mistake I wasted 4hour in identifying it.
希望能帮助那些因为这个愚蠢的错误而面临这个错误的人,我浪费了 4 个小时来识别它。
回答by Damith Ganegoda
I got the same error. I defined java script like this
我得到了同样的错误。我像这样定义了java脚本
<script src="controllers/home.js" />
then I changed to the this
然后我改成了这个
<script src="controllers/home.js"></script>
After this my problem is solved.
在此之后,我的问题就解决了。
回答by sigmapi13
If ALL ELSE fails and your running locally on the MEAN stack like me with gulp...just stop and serve again! I was pulling my hear out meticulously checking everything from all of your posts to no avail till I simply re-ran gulp serve.
如果 ALL ELSE 失败并且您像我一样在 MEAN 堆栈上本地运行 gulp ......只需停止并再次服务!我正在小心翼翼地检查你所有帖子中的所有内容,但无济于事,直到我重新运行 gulp serve。
回答by James
I also encountered this same error and the fix for me was to include my child module in the main module array.
我也遇到了同样的错误,我的解决方法是将我的子模块包含在主模块数组中。
var myApp = angular.module('myApp', ['ngRoute', 'childModuleName']);
回答by Maurizio L
I had similar issue. The fix was ensure that your ctrollers are not only defined within script tags toward the bottom of your index.html just before the closing tag for body but ALSO validating that they are in order of how your folder is structured.
我有类似的问题。修复是确保您的 crollers 不仅在正文结束标记之前的 index.html 底部的脚本标记中定义,而且还验证它们是否按照文件夹的结构顺序排列。
<script src="scripts/app.js"></script>
<script src="scripts/controllers/main.js"></script>
<script src="scripts/controllers/Administration.js"></script>
<script src="scripts/controllers/Leaderboard.js"></script>
<script src="scripts/controllers/Login.js"></script>
<script src="scripts/controllers/registration.js"></script>
回答by sosostris
I also encountered this problem in my project. It eventually worked after I inserted the my-controller.jsinto my karma.conf.jsfile, with the <script>tag.
我在我的项目中也遇到了这个问题。在我将带有标签的文件插入my-controller.js到我的karma.conf.js文件后,它最终起作用了<script>。
Hope this will help. There are quite many reasons that can lead to this problem.
希望这会有所帮助。导致这个问题的原因有很多。

