Sub topic 8
Passing Data Between Components with Inputs and Outputs
This chapter assumes you can already build standalone components, use the imports array, read signals in templates, and write property and event bindings. Inputs and outputs build on those two bindings. A parent sends data down to a child through a property binding on an input. The child reports back up through an event binding on an output.
Declaring inputs and outputs
The input() function creates a read-only signal whose value comes from the parent. input.required() makes the binding mandatory, so if a parent leaves it out, the build fails. The output() function creates an emitter. Call .emit(value) on it to send an event.
import { Component, input, output } from '@angular/core';
@Component({
selector: 'app-todo-item',
template: `
<label [class.done]="done()">
<input type="checkbox" [checked]="done()" (change)="toggled.emit(!done())" />
{{ title() }}
</label>
`,
styles: `.done { text-decoration: line-through; }`
})
export class TodoItemComponent {
title = input.required<string>();
done = input(false);
toggled = output<boolean>();
}Binding from the parent
The parent imports the child, then binds to its inputs with [name] and listens to its outputs with (name). The value passed to emit() arrives in the handler as $event.
import { Component, signal } from '@angular/core';
import { TodoItemComponent } from './todo-item.component';
@Component({
selector: 'app-todo-list',
imports: [TodoItemComponent],
template: `
@for (todo of todos(); track todo.id) {
<app-todo-item
[title]="todo.title"
[done]="todo.done"
(toggled)="setDone(todo.id, $event)" />
}
`
})
export class TodoListComponent {
todos = signal([
{ id: 1, title: 'Learn bindings', done: false },
{ id: 2, title: 'Learn inputs', done: false }
]);
setDone(id: number, done: boolean) {
this.todos.update(list => list.map(t => (t.id === id ? { ...t, done } : t)));
}
}The child never changes done itself. It asks the parent to make the change, and the parent's new state then flows back down through the input. This one-way loop keeps a single owner for each piece of state.
The decorator forms
Older code, and many libraries, use decorators instead. They behave the same way for templates, but inputs declared this way are plain properties rather than signals.
import { Component, EventEmitter, Input, Output } from '@angular/core';
@Component({
selector: 'app-legacy-item',
template: `<span (click)="toggled.emit(!done)">{{ title }}</span>`
})
export class LegacyItemComponent {
@Input({ required: true }) title!: string;
@Input() done = false;
@Output() toggled = new EventEmitter<boolean>();
}Common errors
| Mistake | Symptom | Fix |
|---|---|---|
| Writing `{{ title }}` with `input()` | The text shows the signal function instead of the value | Call it: `title()` |
| Calling `this.done.set(...)` | Compile error, because input signals are read-only | Emit an output, or use `model()` (covered in the next chapter) |
| Child missing from `imports` | "is not a known element" error | Add the child class to the parent's `imports` |
| Required input not bound | Build error for the missing input | Bind it, or give it a default with `input()` |
Exercise
Add a delete output to TodoItemComponent that emits the item's id, and handle it in TodoListComponent so the item is removed. Which new input does the child need, and what does the parent's binding look like?
Show answer
The child needs id = input.required<number>() and delete = output<number>(), plus a button with (click)="delete.emit(id())". The parent binds [id]="todo.id" (delete)="remove($event)" and defines remove(id: number) { this.todos.update(list => list.filter(t => t.id !== id)); }.