Skip to content

Instantly share code, notes, and snippets.

@hakimio
Last active September 6, 2022 07:22
Show Gist options
  • Select an option

  • Save hakimio/ad655f8e91dc759d172ea0a99a0df99a to your computer and use it in GitHub Desktop.

Select an option

Save hakimio/ad655f8e91dc759d172ea0a99a0df99a to your computer and use it in GitHub Desktop.
Global App Injector in Angular below v14
import {Injector} from '@angular/core';
export class AppInjector {
private static injector: Injector;
static setInjector(injector: Injector) {
AppInjector.injector = injector;
}
static getInjector(): Injector {
return AppInjector.injector;
}
}
export function getService<T>(serviceType: Type<T>): T {
const injector = AppInjector.getInjector();
return injector.get(type);
}
platformBrowserDynamic()
.bootstrapModule(AppModule)
.then(
(moduleRef) => {
AppInjector.setInjector(moduleRef.injector);
}
)
.catch(err => console.log(err));
this.reportService = getService(ReportService);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment