# Passing Data with input()

A component declares what it can be given with input(), or input.required() for what it must be given. A parent passes the data with a property binding on the component's tag, and the child reads it like a signal.

Apprentice · Angular · Updated November 3, 2026

## After this lesson you can

- Declare a component's inputs with input() and input.required(), and read them in its template
- Pass data from a parent to a child component with a property binding

## Key takeaways

- `input.required<Category>()` declares an input the parent must give. Leave it off the child's tag, and the build fails.
- `input(false)` declares an optional input with a default value, and its type, here `boolean`, comes from that default.
- An input is a signal: read it by calling it, such as `category()`, and the child updates when the parent passes a new value.
- The parent passes data with a property binding on the child's tag, `[category]="category"`: the child's input on the left, the parent's value on the right.
- Inputs are read-only: the data belongs to the parent, and the child can't change it.

A component that always shows the same thing is of limited use. A `CategoryCard` should show **any** category: Beverages on one card, Condiments on the next. So the card needs to be **given** its category, by the component that uses it.

The component that uses another is its **parent**; the one it uses is its **child**. Data goes from parent to child through the child's **inputs**.

## Declaring an input

```typescript
import { Component, input } from '@angular/core';

export interface Category {
  categoryId: number;
  categoryName: string;
}

@Component({
  imports: [],
  selector: 'app-category-card',
  template: `
    <div class="card" [class.border-primary]="highlighted()">
      <img [src]="'images/categories/' + category().categoryId + '.webp'" class="card-img-top" [alt]="category().categoryName">
      <div class="card-body">
        <h2 class="h5 card-title">{{ category().categoryName }}</h2>
      </div>
    </div>
  `,
})
export class CategoryCard {
  readonly category = input.required<Category>();
  readonly highlighted = input(false);
}
```

- **`input.required<Category>()`** declares an input the parent **must** give: a `Category`.
- **`input(false)`** declares an optional input, with a default value: `false` when the parent doesn't give one. Its type, `boolean`, comes from the default.
- An input is a **signal**: read it by calling it, `category()`. When the parent passes a different value, the signal changes, and the card updates.
- The class and interface are `export`ed, so the parent can use them.

## Passing data from the parent

The parent binds to the input with a **property binding** on the child's tag, as it would to any element's property:

```typescript
import { Component, signal } from '@angular/core';
import { Category, CategoryCard } from './category-card';

@Component({
  imports: [CategoryCard],
  selector: 'app-category-grid',
  template: `
    <div class="row row-cols-3 g-3">
      @for (category of categories(); track category.categoryId) {
        <div class="col">
          <app-category-card [category]="category" [highlighted]="category.categoryId === 1" />
        </div>
      }
    </div>
  `,
})
export class CategoryGrid {
  protected readonly categories = signal<Category[]>([
    { categoryId: 1, categoryName: 'Beverages' },
    { categoryId: 2, categoryName: 'Condiments' },
    { categoryId: 3, categoryName: 'Confections' }
  ]);
}
```

The page shows three cards, *Beverages*, *Condiments* and *Confections*, each with its own picture, and Beverages with a blue border. One `CategoryCard` component, three different categories.

- **`[category]="category"`**: the left side is the child's input; the right side is the parent's value, here the `@for`'s current category.
- **`[highlighted]="..."`** sets the optional input. Leave it off, and the card gets the default, `false`.
- Leave off a **required** input, and the build fails: *Required input 'category' from component CategoryCard must be specified.* You can't forget it.

## Inputs are read-only

A child can't change its inputs: `category` has no `set`. The data belongs to the parent, which can pass a new value at any time. A child that needs to tell the parent something, *"I was clicked"*, sends a message instead: that's the next lesson.

## Check yourself

2 questions to try on the lesson page.

## So what

Inputs turn a component into a template for any data: declare them with `input()`, read them like signals, and let each parent pass in what it wants shown.

## Key terms

- [input()](https://www.ask-codey.com/glossary/input-angular)

## Cheat sheets

- [Passing Data with input()](https://www.ask-codey.com/cheat-sheets/component-communication-in-angular-passing-data-with-input-cheat-sheet)

Source: https://www.ask-codey.com/lessons/component-communication-in-angular-passing-data-with-input
