If I have many components nested inside each other:
ComponentA
|___ Component AA
|___ Component AB
|___ Component ABA
|___ Component ABAA
|___ Component AC
|___ Component AD
and I am currently in the context of Component ABAA
, how might I get the Injector
for Component AA
, or A
?
The @Injectable() decorator defines a class as a service in Angular and allows Angular to inject it into a component as a dependency. Likewise, the @Injectable() decorator indicates that a component, class, pipe, or NgModule has a dependency on a service. The injector is the main mechanism.
We configure these injectors with providers by adding the configuration to either the providers property on the NgModule , Component and Directive decorators or to the viewProviders property on the Component decorator.
Using an InjectionToken objectlink content_copy import { InjectionToken } from '@angular/core'; export const APP_CONFIG = new InjectionToken<AppConfig>('app. config'); The optional type parameter, <AppConfig> , and the token description, app. config , specify the token's purpose.
There are several ways to get the parent injector:
constructor(@SkipSelf() injector:Injector) {}
or
constructor(injector:Injector) {
this.parentInjector = (injector as ReflectiveInjector).parent;
}
I don't know a way to get the injector of siblings of parents. I'm pretty sure there is a better way for the problem you actually try to resolve anyway.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With