在添加指令时,Controller是未定义的
Angular: Controller is undefined when adding a directive
我得到以下错误时,添加一个指令到我的网站:
Error: [ng:areq] Argument 'MainController' is not a function, got undefined
只有当我在我的站点中包含welcome-directive (welcome.js)时才会出现错误。如果导入被移除,说明控制器工作
my index.html正文
<body ng-app="my-app">
<div ng-controller="MainController">
<welcome></welcome>
</div>
<!-- Angular -->
<script src="bower_components/angular/angular.js"></script>
<script src="js/app.js"></script>
<script src="js/controllers/mainController.js"></script>
<!-- Without this line the controller works -->
<script src="js/directives/welcome.js"></script>
</body>
app.js
(function() {
var app = angular.module('my-app', []);
})();
mainController.js
angular.module('my-app', [])
.controller('MainController', ['$scope', function($scope) {
console.log('Controller working');
}]);
welcome.js
angular.module('my-app', [])
.directive("welcome", function() {
return {
restrict: "E",
template: "<div>Test test.</div>"
};
});
您正在重新定义模块。只定义一次模块
作为angular.module('my-app', [])
使用时,它定义了只应该使用一次的模块。当你想取回它的时候。然后使用angular.module('my-app')
angular.module('my-app') //Notice remove []
.directive("welcome", function() {
});
像这样传递第二个参数angular.module('my-app', [])
创建一个新模块,只使用一次。
要检索模块使用var module = angular.module('my-app')
和使用module.controller(...
或module.directive(....
等,
相关文章:
- 为什么“;未定义的“;在JavaScript中结束循环
- 要求未定义JS回调参数
- 如何检查管道中未定义的项目
- TypeError:无法读取属性'推'未定义的JavaScript
- $window.ga在AngularJS事件中未定义
- 未捕获的TypeError无法读取未定义的属性socialsharing
- 为什么grunt contrib connect的中间件选项的第三个参数是未定义的
- 无法获取属性'selectedIndex'的未定义引用或null引用
- 如何消除代码中的未定义和其他问题
- 未捕获的ReferenceError:$未定义
- this.router在AngularJS 2中未定义
- 未捕获的类型错误:无法读取属性'删除'的未定义
- FB 登录 API 的 checkLoginState 在 Angular Controller 中给出未定义
- EmberJS Controller 是未定义的
- 使用 Karma 进行角度测试:注入控制器后,$controller() 未定义
- Argument Controller不是一个函数,未定义-一个页面中有两个Angular应用程序
- EmberRoute.controller返回未定义
- 在Route类中未定义this.controller
- 错误:参数{controller}不是一个函数,未定义
- 在添加指令时,Controller是未定义的