In Angular 5, if I had AbstractClassService
and ExtendedClassService
that extends the abstract, I could do this in my NgModule's providers array:
@NgModule({
providers: [
{provide: AbstractClassService, useClass: ExtendedClassService}
]
})
export class AppModule {}
This would allow me to switch ExtendedClassService
with another for testing or whatever very easily. This can still be done with Angular 6, however there is the new providedIn
option that can be set within the service itself to reduce bundle size:
@Injectable({providedIn: 'root'})
export class ExtendedClassService extends AbstractClassService {}
Is there a way for me to accomplish the same thing I had with Angular 5 while using the new providedIn
? Something like this:
@Injectable({providedIn: 'root', provide: AbstractClassService})
export class ExtendedClassService extends AbstractClassService {}
providedIn
is just for locating the injector which will be responsible for injecting the instance of the classExtendedClassService
, For this it will be root injector. If you don't want root injector for this you can specify any other module .Then the service instance will be created by that injector which includes that module. – AjituseFactory
to provide a factory function that is responsible for creating the instance if you want to create it dynamically. – AjitAbstractClassService
-- because that's is what is injected in the components -- notExtendedClassService
– Padlock