← Back to Angular, page 2

angular-0031 · Medium mid

What are host directives in Angular, and how do they help you compose behaviour?

hostDirectives lets a component or directive attach other standalone directives to its own host element, and choose which of their inputs and outputs to expose. It is composition instead of inheritance, so shared behaviour such as highlighting or tooltips can be reused without extending classes or repeating attributes in templates.

Full answer

Sometimes a component always needs the same extra behaviours on its root element: a tooltip, a focus ring, tracking of clicks. Before Angular 15 you either made a base class and used inheritance, or asked every user of the component to add the attributes in the template. Both were fragile.

hostDirectives fixes this. In the decorator of a component (or another directive) you list standalone directives. Angular applies them to the host element every time the component is created, as if the user had written them.

ts
hostDirectives: [
  { directive: HighlightDirective, inputs: ['color'] },
  { directive: TooltipDirective, inputs: ['appTooltip: hint'] },
]

By default none of the host directive's inputs and outputs are exposed. You choose which are public with inputs and outputs, and you can rename them with 'internalName: publicName'. The directive's lifecycle and host bindings work normally, and the order of execution is the host directives first, then the component or directive that declares them.

Rules and limits: host directives must be standalone, and a host directive cannot add elements, only behaviour on the host element.

Comparison. Inheritance (extends Base) shares code between classes but only one base is allowed and it is tied together at compile time. Composition with host directives lets you mix several small behaviours and keeps each one testable on its own. This is why design-system libraries use them.

typescript
import { Component, Directive, input } from '@angular/core';

@Directive({
  selector: '[appHighlight]',
  host: { '[style.backgroundColor]': 'color()' },
})
export class HighlightDirective {
  color = input('yellow');
}

@Directive({
  selector: '[appTooltip]',
  host: { '[attr.title]': 'text()' },
})
export class TooltipDirective {
  text = input('', { alias: 'appTooltip' });
}

@Component({
  selector: 'app-chip',
  hostDirectives: [
    { directive: HighlightDirective, inputs: ['color'] },
    { directive: TooltipDirective, inputs: ['appTooltip: hint'] },
  ],
  template: `<ng-content />`,
})
export class ChipComponent {}

@Component({
  selector: 'app-leaderboard',
  imports: [ChipComponent],
  template: `<app-chip color="gold" hint="Top scorer">Asha</app-chip>`,
})
export class LeaderboardComponent {}

Angular 20.3, strict template check passes. Use as `<app-chip color="gold" hint="Top scorer">Asha</app-chip>`.

Key points

  • `hostDirectives` applies standalone directives to the host element of a component or directive.
  • Expose inputs and outputs explicitly, and rename them with `'name: alias'`.
  • It replaces most uses of inheritance for shared behaviour.
  • Host directives run before the declaring class.

Common mistakes

  • Expecting the host directive's inputs to be public automatically. They are hidden unless listed.
  • Using a non-standalone directive, which is a compile error.
  • Duplicating the same directive in the template and in `hostDirectives`.

Follow-ups

  • How does this compare with extending a base class?
  • Can a host directive have its own host directives?
  • How would you expose an output from a host directive?

Tips

  • Contrast it with inheritance in one sentence. It is the point of the feature.
  • Name a real use: tooltip, focus management, analytics tracking in a design system.
  • If you have not used it, say so and explain what problem it solves.

Evaluation rubric

  • Explains what host directives are and how they are listed.
  • Knows inputs and outputs must be exposed.
  • Compares with inheritance.