Sub topic 10
Composing directives with hostDirectives
This chapter assumes you can write a standalone attribute directive with @Directive. It also assumes you know how signal input() and output() work, and you can read a component's imports array. If any of these are unfamiliar, review the earlier chapters on inputs and outputs first.
The problem: repeating the same directive
Suppose you have a HighlightDirective and you want every <app-card> to behave as if it carried appHighlight. Without composition, each template author has to remember to write <app-card appHighlight>. Someone will forget, and the cards will behave inconsistently.
Angular's hostDirectives option solves this. A component or directive can declare that other directives are always applied to its host element. The consumer writes only the component's selector.
Applying a host directive
Add hostDirectives to the decorator. Each entry is either a directive class or an object that selects which inputs and outputs to expose.
import { Component } from '@angular/core';
import { HighlightDirective } from './highlight.directive';
@Component({
selector: 'app-card',
hostDirectives: [
{ directive: HighlightDirective, inputs: ['appHighlight: highlight'] },
],
template: `<ng-content />`,
})
export class CardComponent {}Each time <app-card> appears in a template, Angular creates a HighlightDirective instance on that element. You do not list the directive in the consumer's imports, and the consumer does not write appHighlight.
The string 'appHighlight: highlight' has the form 'directiveName: publicName'. It exposes the directive's appHighlight input under the name highlight. Consumers can now write:
<app-card highlight="lightblue">Card content</app-card>What gets exposed
Inputs and outputs of a host directive are private by default. If you leave out inputs and outputs, consumers cannot bind to them. This keeps the component's public API small and deliberate.
| Option | Effect |
|---|---|
| `directive` | The standalone directive class to apply. |
| `inputs` | Inputs to expose. Use `'name'` or `'name: alias'`. |
| `outputs` | Outputs to expose, with the same syntax. |
Host directives must be standalone. Their selectors are not used for matching, so the attribute never has to appear in the template.
Exposing outputs
Outputs follow the same pattern. Here a dropdown component gets two behaviors, and it exposes the click-outside event as closed:
import { Component } from '@angular/core';
import { HighlightDirective } from './highlight.directive';
import { ClickOutsideDirective } from './click-outside.directive';
@Component({
selector: 'app-dropdown',
hostDirectives: [
{ directive: HighlightDirective, inputs: ['appHighlight: highlight'] },
{ directive: ClickOutsideDirective, outputs: ['clickOutside: closed'] },
],
template: `<ng-content />`,
})
export class DropdownComponent {}<app-dropdown highlight="lightblue" (closed)="open = false">Menu</app-dropdown>Directives composing directives
Components are not the only hosts. A directive can also declare hostDirectives, which lets you bundle several small behaviors under one selector:
import { Directive } from '@angular/core';
import { HighlightDirective } from './highlight.directive';
import { ClickOutsideDirective } from './click-outside.directive';
@Directive({
selector: '[appInteractive]',
hostDirectives: [HighlightDirective, ClickOutsideDirective],
})
export class InteractiveDirective {}Writing appInteractive on any element now applies both behaviors. Because no inputs or outputs are listed, none are exposed.
Order and access
- Angular creates host directives before the component or directive that declares them. Their constructors and lifecycle hooks run first.
- The host class can call
inject(HighlightDirective)to get the instance. It can then read or change its state, for exampleactive.set(true). - If the same directive is applied on an element in two ways, for instance as a host directive and again in the template, Angular reports an error. Choose one place to apply it.
Summary
hostDirectives lets a component or directive carry other directives wherever it is used. You choose which inputs and outputs become public, and you can rename them. The consumer's template stays simple, and the composed behavior is applied every time.
Exercise
A component declares hostDirectives: [HighlightDirective] with no inputs list. A consumer writes <app-card appHighlight="red">. Does the color reach the directive? Why or why not?
Show answer
No. Inputs of a host directive are not exposed unless they are listed in the inputs array. Adding inputs: ['appHighlight'] (or an alias such as 'appHighlight: highlight') makes the binding work.
Exercise
Write the hostDirectives entry that applies ClickOutsideDirective to a component and exposes its clickOutside output as dismissed.
Show answer
{ directive: ClickOutsideDirective, outputs: ['clickOutside: dismissed'] }