我将所有AngularJS控制器都放在一个文件controllers.js中.该文件的结构如下:
angular.module('myApp.controllers', [])
.controller('Ctrl1', ['$scope', '$http', function($scope, $http) {
}])
.controller('Ctrl2', ['$scope', '$http', function($scope, $http) }
}])
Run Code Online (Sandbox Code Playgroud)
我想做的是将Ctrl1和Ctrl2放入单独的文件中.然后我会在index.html中包含这两个文件,但是应该如何构建呢?我尝试做这样的事情,它在Web浏览器控制台中抛出一个错误,说它无法找到我的控制器.任何提示?
我搜索了StackOverflow并发现了类似的问题 - 但是,这种语法在Angular之上使用了不同的框架(CoffeeScript),因此我无法遵循.
我正在尝试将我的控制器分成多个文件,但是当我尝试在我的模块中注册它时我得到一个错误:
groupcontroller.coffee
app = angular.module('WebChat', []);
app.controller 'GroupController', ($scope) ->
Run Code Online (Sandbox Code Playgroud)
usercontroller.coffee
app = angular.module('WebChat', []);
app.controller 'UserController', ($scope) ->
Run Code Online (Sandbox Code Playgroud)
错误
错误:参数'GroupController'不是函数,未定义
从文档中我还没有真正得到模块方法所做的事情.它是否用"网聊"键存储我的控制器?
编辑: 似乎传递[]创建一个新模块并覆盖前一个模块
app = angular.module('WebChat', []);
Run Code Online (Sandbox Code Playgroud)
为了防止这种情况,你必须省略[]之类的
app = angular.module('WebChat');
Run Code Online (Sandbox Code Playgroud)