如何找到原因 AngularJS“Argument 'MyCtrl' is not a function, got undefined" [英] How to find a reason AngularJS "Argument 'MyCtrl' is not a function, got undefined"
问题描述
当 AngularJS 因错误Argument 'MyCtrl' is not a function, got undefined"而崩溃时,找到原因可能有点困难.
When AngularJS crashes with an error "Argument 'MyCtrl' is not a function, got undefined" it's may be little bit challenging to find a reason why.
这里我想做一个检查表",出错时应该检查什么
Here I want to make a kind of a "check list", what should you check when got an error
推荐答案
- 带有MyCtrl"的文件是否通过 html 连接?(如果您连接或丑化您的文件,请检查两次)
- Is file with 'MyCtrl' connected via html? (check twice if you concat or uglify your files)
<script src='path/to/controllers.js'></script>
- 'MyCtrl' 是否正确定义?
有几种模式:
app.controller('MyCtrl', ['$scope', function ($scope) {...}])
app.controller('MyCtrl', function ($scope) {...})
var MyCtrl = function ($scope) {...})
- 是否在正确的模块中定义了MyCtrl"?
- MyCtrl's"模块是否添加到应用依赖项?
angular.module('app', ['app.sources']);
如果你多次定义你的模块,你应该按这个顺序定义:
If you define your module multiple times, you should define it in this order:
- 首先定义应该像
angular.module('app.sources', []);
(带有[ ]
)
(with [ ]
)
- 后续定义应该像
angular.module('app.sources');
(不含[ ]
)
(without [ ]
)
重要:声明顺序很重要 - 应先使用 [ ]
进行定义.
Important: Declaration order is important - definition with [ ]
should go first.
- 检查模块是否只定义了一次.您可能忘记在复制粘贴后重命名模块.检查 src 中的 之类的字符串
- Check that module is defined only once. You may have forgotten to rename module after copy-paste. Check src for string like
angular.module('app.sources', []);
检查您的
'ng-app'
.最好只使用其中一个名称类似于ng-app='app'
(换句话说,不要定义多个未命名的 ngApp 指令)
Check your
'ng-app'
. Better to use only one of these with name likeng-app='app'
(In other words do not define multiple unnamed ngApp directives)
您的控制器语法是否适合您的 AngularJS 版本?
(Angular 1.0.x 和 1.2.x 及更高版本的定义有所不同.Angular 版本高于 1.3.x>,你不能声明一个全局构造函数并与 ng-controller 一起使用)
(There is a difference between definition in Angular 1.0.x and 1.2.x and higher. With Angular versions greater than 1.3.x, you cannot declare a global constructor function and use it with ng-controller)
这篇关于如何找到原因 AngularJS“Argument 'MyCtrl' is not a function, got undefined"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!