当 AngularJS 因错误“ Argument 'MyCtrl' is not a function, got undefined ”而崩溃时,找到原因可能有点困难。
这里我想做一个“检查表”,遇到错误应该检查什么
最佳答案
<script src='path/to/controllers.js'></script>
有几种模式:
app.controller('MyCtrl', ['$scope', function ($scope) {...}])
app.controller('MyCtrl', function ($scope) {...})
var MyCtrl = function ($scope) {...})
angular.module('app', ['app.sources']);
angular.module('app.sources', []);
(与
[ ]
)angular.module('app.sources');
(不含
[ ]
)重要 : 声明顺序很重要 - 用 定义
[ ]
应该先走。您可能忘记在复制粘贴后重命名模块。检查 src 中的字符串,如
angular.module('app.sources', []);
'ng-app'
.最好只使用其中一个名称,如 ng-app='app'
(换句话说,不要定义多个未命名的 ngApp 指令)(Angular 1.0.x 和 1.2.x 和更高版本中的定义存在差异。对于大于 1.3.x 的 Angular 版本,您不能声明全局构造函数并将其与 ng-controller 一起使用)
关于javascript - 如何找到原因 AngularJS "Argument ' MyCtrl' 不是函数,未定义“,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26797756/