Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modularize AngularJS application : one or multiple AngularJS modules?

I try to build a modular application using AngularJS. My first idea is to group each module by functionnality with this kind of folder structure :

/core 
    controllers.js
    directives.js
    app.js
/modules
     /users
        controllers.js
        directives.js
     /invoices
        controllers.js
        directives.js
     /messages        
        controllers.js
        directives.js
     ...

Notice that the "core" folder contains basics features that will always be in the app. Others modules can be add or remove independently.

As my application will be large, I also want to use lazy loading. I implemented this solution : http://ify.io/lazy-loading-in-angularjs/ which seems to me actually the easiest way. The problem is that it only does lazy loading for controllers, services, directives ... but not for AngularJS modules.

I know there is another way which permits to lazy load Angular modules (http://blog.getelementsbyidea.com/load-a-module-on-demand-with-angularjs/) but I think it's way too hacky as it uses Angular's core methods.

My question is : does it make sense in my case to use different AngularJS modules for each of my modules, like this :

angular.module('core', ['ngRoute', 'users', 'invoices', 'messages'])
angular.module('users')
angular.module('invoices')
angular.module('messages')

What is the advantage of this approach ? Are AngularJS modules usefull - for now - only for third-party modules for Angular ?

I'm asking this since AngularJS 2.0 will support natively lazy loading. Miško Hevery from Google says "[you] should group by view since views will be lazy loaded in near future", and that we should use one module per application, see it here : https://www.youtube.com/watch?v=ZhfUv0spHCY&t=34m19s

Is it correct for a large application to just use one module for my app like this :

angular.module('core', ['ngRoute']);

And then lazy load my controllers, services and directives based on a route or a view ?

like image 236
tomahim Avatar asked Feb 05 '14 09:02

tomahim


People also ask

Can we have multiple modules in AngularJS?

Yes, you can define multiple modules in angularJS as given below. The modularization in AngularJS helps us to keep the code clarity and easy to understand, as we can combine multiple modules to generate the application.

How many modules should Angular app have?

Every Angular application has at least one module, the root module. You bootstrap that module to launch the application.

Can we have multiple app modules in Angular?

So now, you need to follow some simple steps to use multiple modules in an Angular application. Create a simple application using Angular CLI. If you are going to use Angular for the first time, click here. I am going to create an application which contains three modules: App, Employee, Admin, Home.

Can you have multiple Ng-app directives for a single Angular application?

Explanation: No, an HTML page cannot have multiple "ng-app" directive for bootstrapping multiple AngularJS application. The ng-app directive is used to auto-bootstrap an AngularJS application. And according to AngularJS Documentation, only one AngularJS application can be auto-bootstrapped per HTML document.


1 Answers

We have a similar structure (on a very large app) to the one you are proposing here, except don't have controllers,services and the like bundled up into single .js files. Instead we just separate our concerns with views, services and the like all bundled into a single module. So each meaningful component might looks like:

/my-component
    - i-do-something.js
    - a-view.html
    - a-view-that-is-a-child.html
    - a-view-ctrl.js
    - index.js //the thing that creates a module and returns it
    / tests
         - i-do-something-spec.js
         - a-view-ctrl-spec.js

This is all bundled up into a single app module (named for our org). Then a simple boot module angular.bootstrap(['app'])s the whole thing. We use browserify to compile all this stuff and it has worked nicely so far.

like image 170
SonOfNun Avatar answered Nov 09 '22 17:11

SonOfNun