AI Hints
Home›Angular Tutorial›host Listeners, Template Reference Variables and Practice: A Price List
Angular · Lesson 61 of 120

host Listeners

React to events.

⏱ 2 min read▶ Video at 1:11:55🎓 Free
Watch this lesson in the Angular courseWatch on YouTube ↗Full Angular course

Our highlight directive changed how an element looks. But directives can also react to what the user does, like a click.

  • Change the look
  • React to the user
  • Host listeners

Listening on the host

In the host object, an event name in round brackets listens to the element the directive sits on. The value is the code to run. Global events use a prefix, like window, or document.

keyvalue
'(click)': '...'a click on the element
'(mouseenter)': '...'the mouse moves over it
'(document:keydown)'a key, anywhere
'[class.x]': '...'and bindings, together

toggle.ts

The directive keeps its own signal, called on. A click on the element flips it. And the picked class is added while on is true.

app.tstypescript
import { Directive, signal } from '@angular/core';

@Directive({
  selector: '[appToggle]',
  host: {
    '(click)': 'on.set(!on())',
    '[class.picked]': 'on()',
  },
})
export class Toggle {
  on = signal(false);
}

Picking books

Every paragraph gets the toggle directive. Each one keeps its own state. We click Dune, Circe, and Emma. Then we click Circe again. Run it. Dune, and Emma, are picked. Circe was picked, then unpicked.

app.tstypescript
import { Component } from '@angular/core';
import { Toggle } from './toggle';
import { BOOKS } from './books';

@Component({
  selector: 'app-root',
  imports: [Toggle],
  template: `
    <h3>Click books to pick them</h3>
    @for (b of books; track b.id) {
      <p appToggle>{{ b.title }}</p>
    }
  `,
  styles: `.picked { background: #ffe08a; font-weight: bold; }`,
})
export class App {
  books = BOOKS;
}
Also uses toggle.ts
import { Directive, signal } from '@angular/core';

@Directive({
  selector: '[appToggle]',
  host: {
    '(click)': 'on.set(!on())',
    '[class.picked]': 'on()',
  },
})
export class Toggle {
  on = signal(false);
}
Also uses books.ts
export interface Book {
  id: number;
  title: string;
  author: string;
  price: number;
  stock: number;
}

export const BOOKS: Book[] = [
  { id: 1, title: 'Dune', author: 'Frank Herbert', price: 9.99, stock: 4 },
  { id: 2, title: 'The Hobbit', author: 'J.R.R. Tolkien', price: 8.99, stock: 0 },
  { id: 3, title: 'Circe', author: 'Madeline Miller', price: 10.99, stock: 2 },
  { id: 4, title: 'Emma', author: 'Jane Austen', price: 6.99, stock: 7 },
  { id: 5, title: 'Persuasion', author: 'Jane Austen', price: 7.49, stock: 1 },
];
Also uses actions.json
[{"click": "p:nth-of-type(1)"}, {"click": "p:nth-of-type(3)"}, {"click": "p:nth-of-type(4)"}, {"click": "p:nth-of-type(3)"}]

Summary

Host listeners let a directive react to events on its element, and keep its own state. Next, template reference variables.

Key takeaways
  • '(click)': '...'
  • Own state
  • '[class.picked]'
Next up: Template Reference Variables — name an element