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

Sending Data Up

The parent keeps the state.

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

Now let's use outputs for real. Each card reports a book, and the parent keeps the basket.

  • Cards report
  • The parent keeps the basket
  • Real state

A basket in the parent

The parent shows three cards. When a card emits, the parent adds that book to its basket signal. Titles, and total, are computed from the basket. We add Circe, and then Persuasion. Run it. Both books are in the basket, with the right total.

app.tstypescript
import { Component, signal, computed } from '@angular/core';
import { BookCard } from './book-card';
import { Book, BOOKS } from './books';
@Component({
  selector: 'app-root',
  imports: [BookCard],
  template: `
    @for (b of books.slice(2, 5); track b.id) {
      <app-book-card [book]="b" (added)="basket.update(list => [...list, $event])" />
    }
    <p>Basket: {{ titles() || 'empty' }} · Total £{{ total().toFixed(2) }}</p>
  `,
})
export class App {
  books = BOOKS;
  basket = signal<Book[]>([]);
  titles = computed(() => this.basket().map(b => b.title).join(', '));
  total = computed(() => this.basket().reduce((sum, b) => sum + b.price, 0));
}
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": "app-book-card:nth-of-type(1) button"}, {"click": "app-book-card:nth-of-type(3) button"}]

Who owns what

The cards own nothing, they only show, and report. The parent owns the basket, so every change happens in one place. If another part of the page needs the basket, the parent can pass it down. Later, we'll move shared state into a service.

keyvalue
Cardsshow and report
Parentowns the basket
Changesin one place
Latera service

Summary

Children emit events, and the parent updates its own state. All changes happen in one place. Next, two-way binding, with model.

Key takeaways
  • (added)="..."
  • Parent state
  • computed totals
Next up: model() for Two-Way Binding — both ways at once