Reputation: 2630
About Angular and Feature Modules.
According to this:
When we provide a service in a feature module that is eagerly loaded by our app's root module, it is available for everyone to inject.
Does this mean it's available to all Components of this feature? Or to all components also of other Feature Modules?
Thing is we have a service in a Feature Module and we are able to inject this service in other Feature Module.
Our Feature Module with service:
@NgModule({
imports: [SharedModule],
declarations: [FeatureAComponent],
exports: [FeatureAComponent],
providers: [FeatureAService]
})
export class FeatureAModule { }
Our Feature Module without service:
@NgModule({
imports: [SharedModule],
declarations: [FeatureBComponent],
exports: [FeatureBComponent],
providers: []
})
export class FeatureBModule { }
What is happening here, is that I'm injecting the FeatureAService in FeatureBComponent and it's working, Why?. I was expecting it not to work.
Upvotes: 4
Views: 2514
Reputation: 2630
Ok, now I see. Unless I understood it wrong.
Because my Feature Module is imported in the root module, it makes my provider available to all other Feature Modules.
https://angular.io/guide/ngmodule#!#service-providers
A more specific explaination in the official NgModule FAQs:
Providers listed in the
@NgModule.providers
of a bootstrapped module have application scope. Adding a service provider to@NgModule.providers
effectively publishes the service to the entire application.When you import a module, Angular adds the module's service providers (the contents of its providers list) to the application root injector.
This makes the provider visible to every class in the application that knows the provider's lookup token.
Upvotes: 4