Full answer
Every component or directive is attached to a host element, such as <app-toggle> in the parent's template. Sometimes the component itself must change its host: add a CSS class, set role and tabindex, or react to a click on the whole component. The parent should not need to do this.
Host metadata (recommended):
host: {
role: 'switch',
tabindex: '0',
'[class.on]': 'on()',
'[attr.aria-checked]': 'on()',
'(click)': 'toggle()',
'(keydown.enter)': 'toggle()',
}- A plain key such as
role: 'switch'sets a static attribute. '[class.on]': 'on()'binds a class to an expression.[style.width.px],[attr.x]and[prop]bindings work in the same way.'(click)': 'toggle()'listens to an event, with$eventavailable. Key filters such askeydown.enterwork.- Global targets:
'(window:resize)': 'onResize($event)'or'(document:keydown.escape)': 'close()'.
Decorator form: @HostBinding('class.on') get isOn() { return this.on(); } and @HostListener('click') toggle() {...}. It works and is common in older code, but it splits host behaviour across the class, and it cannot read signals as neatly. The Angular style guide prefers the host object, and a lint rule can enforce it.
Rules and tips:
- Host bindings are checked during change detection of the parent view. Keep them cheap.
- Do not read the DOM in a host binding.
- A listener on the host with a global target (
window:) is removed when the component is destroyed. You do not need to unsubscribe. - For accessibility, a clickable host needs a role,
tabindexand keyboard handling, as in the example. Prefer a nativebuttoninside when you can. - Directives can use
hosttoo, which is how tooltips, focus rings and click-outside directives work.
Compared with ElementRef and Renderer2, host metadata is declarative, and works with server rendering because Angular applies it through the renderer.
import { Component, HostBinding, HostListener, signal } from '@angular/core';
@Component({
selector: 'app-switch',
template: `<ng-content />`,
host: {
role: 'switch',
tabindex: '0',
'[class.on]': 'on()',
'[attr.aria-checked]': 'on()',
'(click)': 'toggle()',
'(keydown.enter)': 'toggle()',
},
})
export class SwitchComponent {
readonly on = signal(false);
toggle(): void {
this.on.update((v) => !v);
}
}
// The same idea with the older decorators:
@Component({ selector: 'app-switch-old', template: `<ng-content />` })
export class OldSwitchComponent {
isOn = false;
@HostBinding('class.on') get on(): boolean {
return this.isOn;
}
@HostListener('click') toggle(): void {
this.isOn = !this.isOn;
}
}Angular 20.3, strict template check passes. A Karma test clicks the host, presses Enter, and checks the class and the ARIA attribute. The decorator form is shown with a comment and was type-checked in the same file.
Key points
- The host element is the component's own tag. Use `host` metadata for attributes, classes, styles and listeners on it.
- `@HostBinding` and `@HostListener` are the older decorator forms.
- Add `role`, `tabindex` and keyboard handling for clickable hosts.
- Global listeners such as `window:resize` are cleaned up automatically.
Common mistakes
- Making the host clickable without keyboard support.
- Reading the DOM inside a host binding expression.
- Adding manual `addEventListener` code when `host` would do.
Follow-ups
- How do you listen to `window:resize` from a directive?
- Why does the style guide prefer `host` metadata?
- How do you set a CSS variable on the host?
Tips
- Show one binding and one listener in the host object.
- Mention accessibility for clickable hosts.
- Say that the decorators still work.
Evaluation rubric
- Explains host element and the metadata forms.
- Uses bindings, listeners and key filters correctly.
- Adds accessibility and lifecycle notes.