Please enable JavaScript.
Coggle requires JavaScript to display documents.
Dependency Resolution - Coggle Diagram
Dependency Resolution
Angular resolves the dependency in two phases
Resolve it using the Element Injector and its parents
If not found in Element Injector
Resolve it against the Module Injector and its parents
Example
Eagerly Loading Component
Angular Search
provider/service/dependency
in Current Component. If not found move upwards to its parents if not found there then move one step up to its grandparents
search starts at the Injector associated with the component in the Element Injector tree
It uses the token to search for dependency(Service) in its Providers array
2 more items...
Lazy Loading Component
For components from the lazy loaded modules, the resolutions start from the Module Injector associated with Lazy Loaded Module
Self
decorator instructs Angular to look for the dependency only in the local injector
local injector is the injector that is part of the current component or directive
SkipSelf
decorator instructs Angular to look for the dependency in the Parent Injector and upwards
It tells Angular not to look for the injector in the local injector, but start from the Parent
you can opposite of the
Self
Optional
Optional marks the dependency as Optional. If the dependency is not found, then it returns null instead of throwing an error text
You will instantly receive the error “No provider for RandomService found in NodeInjector“
Add the
Optional
decorator along with the
Self.
Now, the dependency injection will return null instead of an error.