Host Decorator In Angular
The @Host decorator is one of Angular's Resolution Modifiers. @Self, SkipSelf, and Optional are the others. The DI Framework's dependency resolution is controlled by these Decorators. @Self, @SkipSelf, and @Optional Decorators have already been discussed. In this article, We will learn everything there is to know about @Host.
@Host
It's a little difficult to understand the definition. So let's attempt to make things a little clearer.
- The @Host property only looks for dependencies within the component's template.
- It starts with the current Injector and moves up the Injector hierarchy until it finds the current component's host element.
- It does not look for the dependent in the host element's Providers.
- However, it does look in the host element's ViewProviders.
- When the @Host flag is set, the module injector is never searched.
It resembles @Self in appearance. However, @Self only checks in the current component's Injector.
The @Host, on the other hand, looks for the dependent in the current template.
@Host Example
Let's have a peek at Angular Example. It's the same example as the @Self, @SkipSelf, and @Optional Decorators artcile.
It is made up of three parts. AppComponent, ChildComponent, and GrandChildComponent are three types of components. There is just one testDirective directive. When RandomService is instantiated, it generates a random number.
The random number from the RandomService is displayed in all components and directives.
Make all components and directives' Providers null so that the AppModule always provides RandomService. When you run the app, all of the components and directives should display the same number. In addition, you will only see the RandomService Constructed message in the terminal window once, indicating that only one instance of the service has been built.
Parent & Child Components with @Host Decorator
GrandChildComponent has been introduced to the ChildComponent template.
@Component({ selector: "my-child", providers: [], viewProviders: [], template: ` <div class="box"> <p>ChildComponent => {{ randomNo }}</p> <my-grandchild></my-grandchild> </div> ` }) export class ChildComponent {
Add the @Host decorator to the randomService in the GrandChildComponent now.
export class GrandChildComponent { randomNo; constructor(@Host() private randomService: RandomService) { this.randomNo = randomService?.RandomNo; } }
<div class="box"> <p>ChildComponent => {{ randomNo }}</p> <my-grandchild></my-grandchild> </div>
Directives & @Host Decorator
export class testDirective implements OnInit { constructor( private el: ElementRef, @Host() private randomService: RandomService ) {} This template from GrandChildComponent includes the testDirective. GrandChildComponent is the template's host. template: ` <div class="box"> GrandChildComponent => {{ randomNo }} <div class="dirbox" testdirective="">fdf</div> </div> `,
- Providers array of testDirective
- ViewProviders of the GrandChildComponent
Multiple Directives
<div class="box"> <p>ChildComponent => {{ randomNo }}</p> <div adir=""> <div bdir=""> <div cdir=""></div> </div> </div> </div>
- Providers of cDirective
- Providers of bDirective
- Providers of aDirective
- ViewProviders of ChildComponent
- Providers of bDirective
- Providers of aDirective
- ViewProviders of ChildComponen
Content Projection & @Host Decorator
<div class="box"> <p>AppComponent => {{ randomNo }}</p> <my-child> <my-grandchild></my-grandchild> </my-child> </div>
- Providers of GrandChildComponent
- Providers of ChildComponent
- ViewProviders of AppComponent