Dependency Injection Practical Angular Series: Nir Kaufman
Nir Kaufman Google Developer Expert in Angular. Frontend Tech Lead at Next Insurance. More about me at : nir.life Worldwide speaker and trainer.
Part I Node Injectors
ModuleInjector Meet the Injectors ElementInjector Created for each Directive Created for each Lazy Module @Component/@directive providers/viewProviders @Injectable({providedIn}) @NgModule.providers
ModuleInjector Meet the Injectors ElementInjector Created for each Directive Created for each Lazy Module @Component/@directive providers/viewProviders @Injectable({providedIn}) @NgModule.providers
Control the hierarchy Component Component Component Component Module AppModule PlatformModule NullInjector @Optional()
Control the hierarchy Component Component Component Component Module AppModule PlatformModule NullInjector ElementInjector providedIn: Module | ‘any’ providedIn: ‘root’ providedIn: ‘platform’
Reaching the top AppModule PlatformModule NullInjector Throw an Error Module Module Module Module Module Module providedIn: ’Module’ providedIn: ’any’ providedIn: ‘root’ providedIn `platform`
Injector Con fi guration R3Injector Abstract Injector NodeInjector static create(providers: StaticProvider[]): Injector;
The Provider Token Instructions Your Code Injector.get()
Provider lookup key InjectionToken Class<T> @Inject()
Provider instruction Class Instance useClass What you got What you want What to use Function The Result useFactory Object Keep as-is useValue
Provider instruction Class Instance useClass What you got What you want What to use Function The Result useFactory Object Keep as-is useValue
Provider instruction Class Instance useClass What you got What you want What to use Function The Result useFactory Object Keep as-is useValue
Prevent duplicate instance What you got What you want What to use Class A reference useExisting Already Instantiated
ModuleInjector Meet the Injectors ElementInjector Created for each Directive Created for each Lazy Module @Component/@directive providers/viewProviders @Injectable({providedIn}) @NgModule.providers
ModuleInjector Meet the Injectors ElementInjector Created for each Directive Created for each Lazy Module @Component/@directive providers/viewProviders @Injectable({providedIn}) @NgModule.providers
Control the hierarchy Component Component Component Component Module Component Component Component
Control the hierarchy Component Component Component Component Module Component Component Component NodeInjector NodeInjector NodeInjector NodeInjector
Control the hierarchy Component Component Component Component Module Component Component Component @Self() @SkipSelf() @Host()
Component level providers @Component providers[] @Component viewProviders[] Instance is scoped All Children Instance is scoped to Component View
Part II R3 & Providers
ModuleInjector Meet the Injectors ElementInjector Created for each Directive Created for each Lazy Module @Component/@directive providers/viewProviders @Injectable({providedIn}) @NgModule.providers
ModuleInjector Meet the Injectors ElementInjector Created for each Directive Created for each Lazy Module @Component/@directive providers/viewProviders @Injectable({providedIn}) @NgModule.providers
Control the hierarchy Component Component Component Component Module AppModule PlatformModule NullInjector @Optional()
Control the hierarchy Component Component Component Component Module AppModule PlatformModule NullInjector ElementInjector providedIn: Module | ‘any’ providedIn: ‘root’ providedIn: ‘platform’
Reaching the top AppModule PlatformModule NullInjector Throw an Error Module Module Module Module Module Module providedIn: ’Module’ providedIn: ’any’ providedIn: ‘root’ providedIn `platform`
Injector Con fi guration R3Injector Abstract Injector NodeInjector static create(providers: StaticProvider[]): Injector;
The Provider Token Instructions Your Code Injector.get()
Provider lookup key InjectionToken Class<T> @Inject()
Provider instruction Class Instance useClass What you got What you want What to use Function The Result useFactory Object Keep as-is useValue
Provider instruction Class Instance useClass What you got What you want What to use Function The Result useFactory Object Keep as-is useValue
Provider instruction Class Instance useClass What you got What you want What to use Function The Result useFactory Object Keep as-is useValue
Prevent duplicate instance What you got What you want What to use Class A reference useExisting Already Instantiated
Part III Injection Tokens
Use an `InjectionToken` whenever the type you are injecting is not rei fi ed (does not have a runtime representation) such as when injecting an Interface, callable type, array or parameterized type. From the source
Depend on abstraction Abstraction Your Code Implementation
Wrap external code External code Injection Token
One token - Many providers provider provider provider rovider provide
Open-close principle
Thank You! Nir.life

Angular Dependency Injection