AI Hints
Home›Angular Tutorial›Parent and Child, input() and Required Inputs and Defaults
Angular · Lesson 48 of 120

Required Inputs and Defaults

Inputs you must give.

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

A book card without a book makes no sense. Some inputs must always be given. Angular can insist on it.

  • A card needs a book
  • Some inputs are a must
  • Angular insists

book-card.ts

Input dot required, with the type Book, has no default. The parent must give it. And the template can use book, knowing it's always there.

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

@Component({
  selector: 'app-book-card',
  template: `
    <div class="card">
      <h3>{{ book().title }}</h3>
      <p>{{ book().author }} · £{{ book().price }}</p>
    </div>
  `,
})
export class BookCard {
  book = input.required<Book>();
}

Passing each book

The parent loops over the first three books. For each one, it passes the book down, with square brackets. Run it. Three cards appear, each showing its own book.

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

@Component({
  selector: 'app-root',
  imports: [BookCard],
  template: `
    @for (b of books.slice(0, 3); track b.id) {
      <app-book-card [book]="b" />
    }
  `,
})
export class App {
  books = BOOKS;
}
Also uses book-card.ts
import { Component, input } from '@angular/core';
import { Book } from './books';

@Component({
  selector: 'app-book-card',
  template: `
    <div class="card">
      <h3>{{ book().title }}</h3>
      <p>{{ book().author }} · £{{ book().price }}</p>
    </div>
  `,
})
export class BookCard {
  book = input.required<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 },
];

Forgetting a required input

Here, the parent forgets to pass the book. Run it. The build fails. Angular says, the required input book must be specified. So the mistake is found before anyone sees a broken card.

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

@Component({
  selector: 'app-root',
  imports: [BookCard],
  template: `<app-book-card />`,
})
export class App {}
Output
✘ [ERROR] NG8008: Required input 'book' from component BookCard must be specified.

    src/app/app.ts:7:14:
      7 │   template: `<app-book-card />`,
        ╵               ~~~~~~~~~~~~~
Also uses book-card.ts
import { Component, input } from '@angular/core';
import { Book } from './books';

@Component({
  selector: 'app-book-card',
  template: `
    <div class="card">
      <h3>{{ book().title }}</h3>
      <p>{{ book().author }} · £{{ book().price }}</p>
    </div>
  `,
})
export class BookCard {
  book = input.required<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 },
];

Summary

Input dot required has no default, and Angular stops the build if the parent forgets it. Next, outputs.

Key takeaways
  • input.required<Book>()
  • Must be given
  • Build fails if missing
Next up: output() — events to the parent