AI Hints
Home›Angular Tutorial›Content Projection, Building a Small Component Tree and Practice: A Star Rating
Angular · Lesson 53 of 120

Building a Small Component Tree

Three levels.

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

Let's put inputs, and outputs, together, across three levels. App, a book list, and book cards.

  • Three levels
  • Inputs down
  • Outputs up

The tree

App owns the books, and the basket count. It passes the books down to the book list. The list passes each book down to a book card. A click on a card goes up to the list. And the list passes it up again, to App.

  1. App: books + count
  2. → BookList [books]
  3. → BookCard [book]
  4. BookCard (added) ↑
  5. BookList (added) ↑ App

book-list.ts

Book list takes a books input, and declares its own added output. For each book, it shows a card. When a card emits, the list simply emits the same book upwards.

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

@Component({
  selector: 'app-book-list',
  imports: [BookCard],
  template: `
    @for (b of books(); track b.id) {
      <app-book-card [book]="b" (added)="added.emit($event)" />
    }
  `,
})
export class BookList {
  books = input.required<Book[]>();
  added = output<Book>();
}

App at the top

App passes the books in stock down to the list. When added arrives, it counts one more book in the basket. We add two books. Run it. The clicks travelled up two levels, and the count is two.

app.tstypescript
import { Component, signal } from '@angular/core';
import { BookList } from './book-list';
import { BOOKS } from './books';

@Component({
  selector: 'app-root',
  imports: [BookList],
  template: `
    <h2>Basket: {{ count() }} books</h2>
    <app-book-list [books]="books" (added)="count.update(c => c + 1)" />
  `,
})
export class App {
  books = BOOKS.filter(b => b.stock > 0);
  count = signal(0);
}
Also uses book-list.ts
import { Component, input, output } from '@angular/core';
import { BookCard } from './book-card';
import { Book } from './books';

@Component({
  selector: 'app-book-list',
  imports: [BookCard],
  template: `
    @for (b of books(); track b.id) {
      <app-book-card [book]="b" (added)="added.emit($event)" />
    }
  `,
})
export class BookList {
  books = input.required<Book[]>();
  added = output<Book>();
}
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 button"}, {"click": "app-book-card:nth-of-type(3) button"}]

Summary

Inputs carry data down the tree, and outputs carry events back up, one level at a time. Next, some practice.

Key takeaways
  • [books]
  • (added)
  • Up, one level at a time
Next up: Practice: A Star Rating — your turn