AngularJS: How do I create controllers in multiple files

angularjs, coffeescript

Solution

Check other places in your code where you're referencing 'GroupController' (probably in your route). Chances are you have it as there as a variable, but when you declare a controller inside a module you'll have to wrap it quotes. EG:

MyCtrl1() = -> ()
...
$routeProvider.when('/view1', {templateUrl: 'partials/partial1.html', controller: MyCtrl1})

works fine because MyCtrl1 is a variable. But when declaring controllers in a module as you are:

app = angular.module('WebChat', []);
app.controller 'GroupController', ($scope) ->
   # ...

$routeProvider.when('/view1', {templateUrl: 'partials/partial1.html', controller: 'GroupController'})

'GroupController' needs quotes in the route.

Problem

I'm trying to split my controllers into multiple files, but when i try to register them at my module im getting an error: groupcontroller.coffee ``` app = angular.module('WebChat', []); app.controller 'GroupController', ($scope) -> ``` usercontroller.coffee ``` app = angular.module('WebChat', []); app.controller 'UserController', ($scope) -> ``` Error Error: Argument 'GroupController' is not a function, got undefined From the documentation I dont really get what the module method does anyway. Does it store my controller with the key 'Webchat'? Edit: It also seems that passing [] creates a new module and overwrites the previous one ``` app = angular.module('WebChat', []); ``` To prevent this, you have to leave out the [] like ``` app = angular.module('WebChat'); ```

Original source