0

I have several modules in my application, every module definition in separated js file:

angular.module('app', ['app.module1']);

angular.module('app.module1', ['app.module1.module2']);

angular.module('app.module1.module2', []);

And then I want to create controller for last module:

angular.module('app.module1.module2').controller('myController', function(){});

In this case I have error

Module 'app.module1.module2' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.

Could anyoune explain where is the problem? Sorry for newbie question. Note: every definition in it's own js file.

4
  • 1
    Where are you creating the controller on app.module1.module2? Is it in a different file than where you're creating the app.module.module2 module? If it's in a different file the module declaration file needs to load first. Commented May 20, 2014 at 23:16
  • Works here: plnkr.co/edit/L310PHMhewEZMakGKBGv?p=preview You must be trying to create the controller before all modules have loaded as per @theJoeBiz Commented May 20, 2014 at 23:18
  • @theJoeBiz, yes, they are different files. But I thought that module loading happends only after angular bootstrapping Commented May 20, 2014 at 23:19
  • Thanks for your points, guys, I'll try to fix loading order Commented May 20, 2014 at 23:20

1 Answer 1

1

You cant use a module before it is created, so you must be carefull in which order you load then ,load the module definition first then you can reopen the module in a file loaded after the module definition file.

However it makes more sense to stick to one one module per file. That way you dont have to care in which order your module are declared.

so you could write thing instead :

angular.module('app.module1.module2.controllers',[])
.controller('myController', function(){});

then

angular.module('app.module1.module2', ['app.module1.module2.controllers']);

even if you load app.module1.module2 after app.module1.module2.controllers it will work.

Sign up to request clarification or add additional context in comments.

3 Comments

so do you offer to define module in one file? I have multiple modules with multiple dependencies, so I have to keep in mind testing and scaling.
yes defines one module per file OR keep track of the order in which modules are defined.Testing is not an issue.You can still mock dependencies.
I think that defining of module per file will be more scalable, so I'm going to fix order, thanks

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.