How to use Dependency Injection (DI) correctly in Angular2?

Broad question, TL;DR version @Injectable() is a decorator which tells the typescript that decorated class has dependencies and does not mean that this class can be injected in some other. And then TypeScript understands that it needs to Inject the required metadata into decorated class when constructing, by using the imported dependencies. bootstrap(app, [service]) bootstrap() … Read more

Getting instance of service without constructor injection

In the updated Angular where ngModules are used, you can create a variable available anywhere in the code: Add this code in app.module.ts import { Injector, NgModule } from ‘@angular/core’; export let AppInjector: Injector; export class AppModule { constructor(private injector: Injector) { AppInjector = this.injector; } } Now, you can use the AppInjector to find … Read more