Skip to content

Instantly share code, notes, and snippets.

@alxhub
Created June 5, 2018 18:24
Show Gist options
  • Save alxhub/dd3fb7c09f347d6c21972b1088e1ae24 to your computer and use it in GitHub Desktop.
Save alxhub/dd3fb7c09f347d6c21972b1088e1ae24 to your computer and use it in GitHub Desktop.
@Injectable({providedIn: ServiceModule})
export class Service {}
@NgModule({}) export class ServiceModule {}
@NgModule({
imports: [ServiceModule]
})
export class AppModule {}
@Component({})
export class Cmp {
constructor(svc: Service) {}
}
providers: [Service]
1) telling Angular which injector should create the service
2) actually registering the service with the injector
3) specifying when the service should be loaded (in terms of
lazy loading)
export interface IService {
}
export const SERVICE_TOKEN = new InjectionToken<IService>({
providedIn: 'root',
factory: () => inject(ServiceImpl)
});
// useValue: foo
factory: () => foo,
// useExisting: SomeToken
factory: () => inject(SomeToken)
// useClass: SomeClass
factory: () => new SomeClass(inject(DepA), inject(DepB)),
// useFactory: factoryFn, deps: [DepA, DepB]
factory: () => factoryFn(inject(DepA), inject(DepB))
//{provide: SERVICE_TOKEN, useClass: ServiceImpl}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment