user431619
user431619

Reputation:

Separation of AngularJS controller files?

I'm getting an error when running my app: Argument 'AppCtrl' is not a function, got undefined - I believe it has something to do with the separation of controller files?

Ok, so I have in my first file: app.js this:

angular.module('zerochili', [
    'ionic',
    'zerochili.controllers',
    'zerochili.services',
    'zerochili.directives'
])

Then I have some different controller files - Lets take the file there the AppCtrl is - This looks like the following:

angular.module('zerochili.controllers', [])

.controller('AppCtrl', ['$scope', '$ionicModal', '$timeout', function ($scope, $ionicModal, $timeout){


}])

And another file fx like so:

angular.module('zerochili.controllers', [])
.controller('LoginCtrl', ['$scope', '$state', '$ionicPopup', '$timeout', function($scope, $state, $ionicPopup, $timeout){

}]);

What am I doing wrong? Can't seem to quite figure it out?

Upvotes: 0

Views: 54

Answers (2)

ScottL
ScottL

Reputation: 1755

You can think of your app.js as the part where you define the module. The controller files should be adding to the module (not have ", []" as this creates the module).

You should probably be doing this in your controllers

angular.module('zerochili')
.controller('AppCtrl', etc.

So, remove these: 'zerochili.controllers', 'zerochili.services', 'zerochili.directives' from the app.js and add your controllers,services and directives to the 'zerochili' module like above.

If you want to keep, for example, 'zerochili.controllers' make sure that you don't create this module twice, i.e. have angular.module('zerochili.controllers', []) twice.

Upvotes: 1

Dan M. CISSOKHO
Dan M. CISSOKHO

Reputation: 1065

It's only because you are creating a new modul with []

you can put at the end of your app.js

angular.module('zerochili.controllers', []);
angular.module('zerochili.directives', []);
angular.module('zerochili.services', []);

and then use it like this:

angular.module('zerochili.controllers')...

Upvotes: 1

Related Questions