AI Hints
Home›Angular Tutorial›output(), Sending Data Up and model() for Two-Way Binding
Angular · Lesson 49 of 120

output()

Events to the parent.

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

When the user clicks add, on a book card, the card shouldn't change the basket itself. It should tell its parent. That's what outputs are for.

  • A click in the child
  • Tell the parent
  • Outputs

Declaring outputs

In the child, output creates an event, with the type of data it sends. Emit sends the event, with a value. The parent listens with round brackets, like a normal click. And dollar event is the value that was sent.

keyvalue
added = output<Book>()declare it
added.emit(book)send it
(added)="onAdded($event)"listen in the parent
$eventthe sent value

book-card.ts

Added is an output, that sends a Book. When the button is clicked, the card emits its book. That's all it does. It doesn't know what happens next.

book-card.tstypescript
import { Component, input, output } from '@angular/core';
import { Book } from './books';

@Component({
  selector: 'app-book-card',
  template: `
    <div class="card">
      <b>{{ book().title }}</b> · £{{ book().price }}
      <button (click)="added.emit(book())">Add</button>
    </div>
  `,
})
export class BookCard {
  book = input.required<Book>();
  added = output<Book>();
}

Listening in the parent

The parent listens to added, with round brackets. Dollar event is the book that was sent, and on added receives it. We click add. Run it. The parent received Dune, from its child.

app.tstypescript
import { Component } from '@angular/core';
import { BookCard } from './book-card';
import { Book, BOOKS } from './books';

@Component({
  selector: 'app-root',
  imports: [BookCard],
  template: `<app-book-card [book]="books[0]" (added)="onAdded($event)" />`,
})
export class App {
  books = BOOKS;

  onAdded(book: Book) {
    console.log(`The child sent: ${book.title}`);
  }
}
Output
The child sent: Dune
Also uses book-card.ts
import { Component, input, output } from '@angular/core';
import { Book } from './books';

@Component({
  selector: 'app-book-card',
  template: `
    <div class="card">
      <b>{{ book().title }}</b> · £{{ book().price }}
      <button (click)="added.emit(book())">Add</button>
    </div>
  `,
})
export class BookCard {
  book = input.required<Book>();
  added = output<Book>();
}
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": "button"}]

Summary

Output declares an event, emit sends it, and the parent listens with round brackets, and dollar event. Next, sending data up into the parent's state.

Key takeaways
  • output<Book>()
  • emit()
  • (added)="..."
Next up: Sending Data Up — the parent keeps the state