-
Notifications
You must be signed in to change notification settings - Fork 27.3k
proposed: implicit namespacing #2767
Description
Building large Angular apps often means making a "main" app and many "sub" apps that the main app depends on.
angular.module('main', ['appone', 'apptwo', 'appthree']);
Now you have to namespace everything in your app to avoid collisions. For example, if appone
had a CoolCtrl
and apptwo
had a CoolCtrl
, the second would overwrite the first. The current solution is to name them ap1CoolCtrl
and ap2CoolCtrl
respectively. This manual namespacing is tedious, error prone, and reduces portability.
I propose we tighten up the injector, so that the only components (services, filters, etc.) available for dependency injection are those that are either declared in the module itself or one of its dependencies. So in our example, if appone
had a NeatService
that service could only be injected into appone
and main
, but not in apptwo
since apptwo
does not explicitly depend on appone
. Now you don't have to namespace all of your own Angular components.
The other piece is how to disambiguate which app you're using in your templates. I propose a new directive named ng-use
:
<div ng-use="appone">
<div ng-controller="CoolCtrl"/>
</div>
<div ng-use="apptwo">
<div ng-controller="CoolCtrl"/>
</div>
Any ng-includes etc. would propagate the currently used module.
If no ng-use
is specified, it implicitly uses the module declared by the bootstrapping (ng-app) which is the main
app.
Angular should then throw if a component is defined more than once.
And finally, for the sake of the main
app, Angular could automatically namespace the components with their module name:
angular.module('main').controller('Router', function(apponeCoolCtrl, apptwoCoolCtrl){
//yay automatic namespacing
});
Thoughts/feedback welcome.