I am aware that Angular2 default DI Context strategy is to enrich an application-wide Dependency Injection Context. But I would like some Injectables
to not be globally available.
Here is a concrete example of what I am trying to achieve.
There is probably a doc explaining how to do so but I have not been able to find it.
I would like to create a ServiceModule that would differentiate Public/Exported services from Private services.
@Injectable() export class PrivateService{} // Not available in AppModule
@Injectable() export class PubliclyExportedService{ // Available in AppModule
constructor(private privateService: PrivateService) { }
}
// The imported module
@NgModule({
exports: [PubliclyExportedService],
declarations: [PubliclyExportedService],
providers: [
PubliclyExportedService,
PrivateService
]
})
export class ServiceModule {}
// The main module
@NgModule({
imports: [ServiceModule]
})
export class AppModule { }
The goal is to use the dependency injection so that PubliclyExportedService
can use PrivateService
but any class outside of the ServiceModule
would not be capable of injecting the PrivateService
.
Any help is greatly appreciated