Modules and namespace / name collision in AngularJS

As of today, AngularJS modules do not provide any sort of namespacing that would prevent collisions between objects in different modules. The reason is that an AngularJS app has a single injector that holds names for all objects without respect to module names.

The AngularJS Developer Guide says:

To manage the responsibility of dependency creation, each Angular
application has an injector. The injector is a service locator that is
responsible for construction and lookup of dependencies.

As you’ve mentioned, nasty bugs can result when injecting modules into your main/app module. When collisions happen they are silent and the winner is determined by whichever was the last module injected.

So no, there’s not a built in way of avoiding these collisions. Maybe this will happen in the future. For large apps where this problem becomes more likely, you’re right that naming conventions are your best tool. Consider whether objects belonging to a module or function area might use a short prefix.

Leave a Comment