Video lecture
Watch on YouTube(opens in a new tab)Sub topic 12
Passing Data Between Components
This chapter assumes you have read the earlier chapters on standalone components, template binding, @if/@for, and signals. You should already know how to call a signal in a template, for example count().
Real apps are trees of components. A parent component owns some data and passes it down to a child component. The child reports user actions back up. Angular uses inputs for data going down and outputs for events going up.
The child: declaring inputs and outputs
The input() function creates a read-only signal whose value is set by the parent. The output() function creates an emitter, and the child calls .emit() on it to send an event.
// src/app/todo-list.ts
import { Component, input, output } from '@angular/core';
@Component({
selector: 'app-todo-list',
template: `
@if (items().length === 0) {
<p>Nothing to do.</p>
} @else {
<ul>
@for (item of items(); track item) {
<li (click)="selected.emit(item)">{{ item }}</li>
}
</ul>
}
`,
})
export class TodoList {
items = input<string[]>([]);
selected = output<string>();
}input<string[]>([]) sets the type and a default value. The default applies when the parent binds nothing. If the parent must always supply a value, use input.required<string[]>() instead. The compiler then reports an error when the binding is missing.
The parent: binding to the child
The parent uses property binding, [items], to pass data down. It uses event binding, (selected), to listen for the child's output. Inside an event binding, $event holds the value passed to emit().
// src/app/app.ts
import { Component, signal } from '@angular/core';
import { TodoList } from './todo-list';
@Component({
selector: 'app-root',
imports: [TodoList],
template: `
<app-todo-list [items]="todos()" (selected)="onSelected($event)" />
@if (picked()) {
<p>You picked: {{ picked() }}</p>
}
`,
})
export class App {
todos = signal(['Write code', 'Test code', 'Ship code']);
picked = signal<string | null>(null);
onSelected(item: string) {
this.picked.set(item);
}
}When todos changes, Angular updates the child's items input. When a user clicks a list item, the child emits the item, and the parent stores it in picked.
Common errors
| Symptom | Cause | Fix |
|---|---|---|
| `'app-todo-list' is not a known element` | The child isn't in the parent's `imports` array | Add `TodoList` to `imports` |
| Template shows function text or nothing at all | Wrote `items` instead of `items()` | Call input signals like functions |
| `Can't bind to 'items'` | The input name doesn't match, or the property isn't declared with `input()` | Check spelling and the declaration |
| Error calling `.set()` on an input | Inputs are read-only in the child | Let the parent change the value, or use `model()` for two-way binding |
| `$event` type error | The `output<T>()` type doesn't match the handler's parameter | Keep both types the same |
Aliasing names
You can give an input or output a different public name with input([], { alias: 'todoItems' }) or output<string>({ alias: 'picked' }). Use aliases sparingly, because then the property name in the class and the name used in templates no longer match.
Exercise
Add a required title input of type string to TodoList and display it in an <h2>. What must the parent template now include, and what happens if it is missing?
Show answer
Declare title = input.required<string>(); and add <h2>{{ title() }}</h2> to the template. The parent must bind it, for example <app-todo-list title="My tasks" [items]="todos()" />. If the binding is missing, the Angular compiler reports an error that the required input 'title' is not set.
Exercise
Add a "Clear" button to TodoList that tells the parent to empty the list. Which API do you use, and what does the parent do?
Show answer
Add cleared = output<void>(); and a button <button (click)="cleared.emit()">Clear</button>. The parent binds (cleared)="todos.set([])". The child cannot change its items input itself, because inputs are read-only.