0

I fount out in the official angularjs documents that

angular.module('foo', []) makes the definition of foo module, and

angular.module('foo') is calling (not making) foo module.

But I could not start my app when I wrote the code below,

app/controllers/file1.js var app = angular.module('app.controller', []); app/controllers/file2.js var app = angular.module('app.controller');

and, it worked when I only changed those two declarations:

app/controllers/file1.js var app = angular.module('app.controller'); app/controllers/file2.js var app = angular.module('app.controller', []);

so... I am wondering that

  • how the order of loading module is decided
  • how should I do when I want to use same module on two or more files

Thanks.

qsona
  • 67
  • 6

1 Answers1

2

It's pretty straightforward: you must create the module before to be able to use it.

It's clearly a bad idea to create it in a controller file; use a separate file for this purpose, in which you will also be able to make the global configuration (myModule.config()) of your project, for instance. In your case:

/** In "app/controller.js" **/
angular.module('app.controller', []); // Creation of the module

/** In "app/controllers/file1.js" **/
/** In "app/controllers/file2.js" **/
angular.module('app.controller'); // Use of the (already existing) module

The file app/controller.js should be called first. Then, the order of the other files doesn't matter.

Blackhole
  • 20,129
  • 7
  • 70
  • 68
  • thanks. May I ask an additional question? I don't know how to call "app/controller.js" first in your example. Could you show some examples about it? AngularJS doesn't have main method, so it can be in the global configuration...? – qsona Jun 02 '14 at 14:18
  • Just put the tags ` – Blackhole Jun 02 '14 at 18:34
  • Thanks, I got it. I didn't realize it because I was using brunch.io (and its seed for angularjs) and I didn't need to write script tags. I 'll check the brunch.io's config. Thank you for helpful infomations. – qsona Jun 03 '14 at 15:22