Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a parent components injector?

Tags:

angular

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?

like image 967
Aristata Avatar asked May 05 '16 15:05

Aristata


People also ask

How do you inject a service into a component?

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.

How will you configure module injector?

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.

How can you configure the injector to use an existing object for a token?

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.


1 Answers

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.

like image 135
Günter Zöchbauer Avatar answered Oct 01 '22 22:10

Günter Zöchbauer