Reputation: 2740
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?
Upvotes: 0
Views: 100
Reputation: 21642
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?
Upvotes: 1