Angular Directives Created: 16 Jan 2026 Updated: 16 Jan 2026

Interactive Angular Directives: Powering UI with @HostBinding and @HostListener

In our previous look at directives, we learned how to use Renderer2 to apply styles automatically. However, what if you want your element to react when a user interacts with it?

To make directives interactive, Angular provides two powerful decorators: @HostBinding and @HostListener.

Key Concepts

  1. @HostBinding: Think of this as a way to control the "state" or "appearance" of the element. It links a variable in your TypeScript code to a property (like a CSS class or style) on the HTML element.
  2. @HostListener: Think of this as the "ears" of your directive. It waits for events (like clicks, mouse movements, or keystrokes) to happen on the element and runs a function when they do.

Practical Example: The Interactive Highlight Directive

Let's build a directive called appHoverEffect. It will automatically add a CSS class when the mouse enters the element and remove it when the mouse leaves.

1. Define the CSS Class

First, define the style in your styles.css.

styles.css

.active-hover {
background-color: #e0f2fe;
border: 2px solid #0ea5e9;
cursor: pointer;
transition: all 0.2s ease;
}

2. Create the Interactive Directive

We will use @HostBinding to toggle the class and @HostListener to detect the mouse movement.

hover-effect.directive.ts

import { Directive, HostBinding, HostListener } from '@angular/core';

@Directive({
selector: '[appHoverEffect]',
standalone: true
})
export class HoverEffectDirective {

// We bind the 'active-hover' CSS class to the boolean variable 'isHovering'
// When 'isHovering' is true, the class is added. When false, it is removed.
@HostBinding('class.active-hover') isHovering: boolean = false;

// Listen for the 'mouseenter' event
@HostListener('mouseenter') onMouseEnter() {
this.isHovering = true;
}

// Listen for the 'mouseleave' event
@HostListener('mouseleave') onMouseLeave() {
this.isHovering = false;
}
}

3. Usage in the Template

You don't need to write any complex logic in your HTML. Just add the attribute to any element.

app.component.html

<div class="card-container">
<div class="card" appHoverEffect>
<h3>Interactive Card</h3>
<p>Hover over me to see @HostBinding and @HostListener in action!</p>
</div>

<div class="card">
<h3>Standard Card</h3>
<p>I don't have the directive, so I stay static.</p>
</div>
</div>

Why is this better?

  1. Cleaner Code: You don't need to inject Renderer2 or ElementRef for simple class toggling. Angular handles the "magic" for you behind the scenes.
  2. No Manual Event Listeners: You don't need to use addEventListener or worry about cleaning up listeners when the component is destroyed. Angular manages the lifecycle of @HostListener automatically.
  3. Readability: Anyone looking at your code can immediately see which events the directive listens to and which properties it modifies.

Summary

  1. Use @HostBinding to change the element's properties (like class, style, or disabled status).
  2. Use @HostListener to react to user actions (like click, mouseover, or keydown).

By combining these two, you can turn any boring HTML tag into a smart, interactive component with just a few lines of code!

Share this lesson: