Let's say I have 3 modules:
angular.module('A', [])
angular.module('B', ['A'])
angular.module('C', ['B', 'A'])
In terms of dependency injection, will B
and C
share the same instance of module A
, or will separate instances be injected into each of them?
To summarize your question, answer would be only one instance.
In fact it is this way. Angular app resolves dependencies via injector. Only one injector is created per app. Technically you can have only one
ng-app
, but you can have multiple apps by using manual bootstrapping, in that case there will be an injector created for each app and those 2 apps will not share any dependencies.In general case where there is only rootElement bootstrapped as an angular app. It resolves the modules starting the module that has been bootstrapped (starting from the bottom of the dependency chain). Any services/controllers/filters etc registered under that module or any dependent modules under that dependency chain will be bundled together in the injector cache(instantiated lazily when injected) just once. For example say you have a service
myService
registered under moduleA
. No matter how many places you listA
as dependency ultimately only one instance of its constructor will be available, and service being a singleton everyone gets the same singleton instance.Official Doc: