0

I have read a lot of descriptions on modules in angularjs and am perplexed as to what their purpose really is. Let me explain my situation with a bit of code, this is firstly how I am supposed to do according to best practices:

angular.module('secondModule',[]).controller('myController',function(){
    //do something
};

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

However as I see it (and from reading parts of the angularjs source code) this is under the hood exactly the same as:

angular.module('app',[]).controller('myController',function(){
    //do something
};

Basically no matter in which module I create my controllers, filters, directives etc. they will still all go into a single global bucket, once a controller is created it is simply registered to the global controller list and has no connection what so ever to the module it was created with.

The only thing I seem to see that the modules are good for is specifying in which order they should be interpreted by the browser (by specifying dependencies in the module declaration).

So the question is simply, what am I missing, why should I use modules when whatever I create using those modules are still sitting in the same global bucket?

1 Answer 1

1

In a simple situation such as yours, there probably is no practical purpose to using modules, assuming that you are not planning to re-use your code or package it up for third-party use. On the other hand, take the example of a system that I'm currently working on.

We have this block of functionality that allows either end users or admins to manipulate some security settings. Basically the user can tweak his own settings, or an admin could tweak them for the user. The process of the actual tweaking for end-user and admin is the same, but they are in separate AngularJS applications.

So we put the tweak code, so to speak, in a separate module and then call that module from the two separate AngularJS applications. The admins have their own app that now calls the tweak module, and the users have their own app that also now loads the tweak module.

One piece of code to update, but it is used by two different applications.

In our particular application it is two separate facets of one Java application that has AngularJS integrated through the use of Spring MVC for manipulating the data, but the two AngularJS applications are, by necessity, wholly separate from each other. Yet they use the bulk of the same code....

Does that make sense?

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

2 Comments

That makes perfect sense, a good reason to use modules. And my example was only an exemplification. In my actual case though I will use requirejs to load my "modules". So the tweak code in you example would be in one or more js files and those files would be required by the different end-user and admin settings interfaces. Then it still would not matter if I only had one "global" angular module.
Right, if you are using RequireJS, then you are already handling this a different way. Good luck.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.