Sub topic 1
How parents and children communicate
Prerequisites
This chapter assumes you can already do the following:
- Write TypeScript classes and use generics such as
Array<string>. - Build Angular standalone components and use the basic template syntax.
- Use property binding
[prop]and event binding(event)in a template. - Read a signal by calling it, as in
count().
The chapter is a map of the whole topic. The later chapters cover each tool in detail.
Two directions, two mechanisms
An Angular application is a tree of components. A parent places a child in its template, and the child may place its own children below it. Most real features need two kinds of traffic between a parent and a child:
- Data down. The parent gives the child values to display or work with, such as a user name or a list of items.
- Events up. The child tells the parent that something happened, such as a click, a finished save, or a changed selection.
Angular gives each direction its own mechanism:
- An input is a property of the child that the parent sets through a template binding. You declare it with the
input()function. - An output is an event source on the child that the parent listens to. You declare it with the
output()function.
Together they form the child's public contract. The parent does not reach into the child's fields or call its methods. It sets inputs and handles outputs. The child does not know which parent uses it, or whether any parent is listening.
A third tool, model(), combines an input and an output so that a value can flow in both directions. A later chapter covers it. For now, treat it as shorthand for a matching pair.
A signal-based approach
The input() function returns a signal, a value holder that notifies readers when it changes. You read an input by calling it: name(). This has three practical effects:
- The template and any
computed()oreffect()that reads the input updates automatically when the parent passes a new value. - The input is read-only inside the child. The signal has no
set()orupdate()method, so the child cannot overwrite what the parent supplied. - The compiler knows the input's type and whether it is required, so a parent that forgets a required value fails at build time.
The older @Input() and @Output() decorators still work, and a later chapter shows how to migrate from them. This tutorial uses the function-based API throughout.
A first complete round trip
The following child declares one input and one output. The input step sets how much to add on each click, and it defaults to 1. The output changed reports the new total.
import { Component, input, output } from '@angular/core';
@Component({
selector: 'app-counter',
template: `
<button (click)="increment()">+{{ step() }}</button>
<span>{{ count }}</span>
`,
})
export class Counter {
step = input(1);
changed = output<number>();
count = 0;
increment() {
this.count += this.step();
this.changed.emit(this.count);
}
}The parent below uses the counter. It keeps the step size and the latest total in its own signals.
import { Component, signal } from '@angular/core';
import { Counter } from './counter';
@Component({
selector: 'app-dashboard',
imports: [Counter],
template: `
<app-counter [step]="stepSize()" (changed)="onChanged($event)" />
<p>Latest total reported: {{ total() }}</p>
`,
})
export class Dashboard {
stepSize = signal(5);
total = signal(0);
onChanged(value: number) {
this.total.set(value);
}
}Three things happen here:
[step]="stepSize()"is property binding. Angular reads the parent's signal and writes the value into the child'sstepinput.(click)inside the child callsincrement(), which callsthis.changed.emit(this.count).(changed)="onChanged($event)"is event binding. The argument toemit()becomes$eventin the parent's handler. Becausechangedis declared asoutput<number>(),$eventis typed asnumber.
The diagram shows the same round trip as a sequence.
What each side owns
The split follows one rule: whoever owns a piece of state is the only one that changes it.
- The parent owns
stepSizeandtotal. The child may readstep()but cannot write it. - The child owns
count, which is private to its own behavior. It shares it with the world only by emitting it. - When the child wants the parent to change something, it emits an event. The parent decides whether to change anything.
This is why the contract stays predictable. When a value looks wrong, you know where to look. For an input, look at the parent that binds it. For an event, look at the child that emits it.
Choosing the right tool
| Need | Tool | Declared in the child with | Used in the parent with |
|---|---|---|---|
| Pass a value down | Input | `input()` or `input.required()` | `[name]="expr"` or `name="text"` |
| Report an event up | Output | `output<T>()` | `(name)="handler($event)"` |
| Keep one value in sync both ways | Model | `model()` | `[(name)]="prop"` |
| Expose a stream as an event | RxJS interop | `outputFromObservable()` | `(name)="handler($event)"` |
Behavior that surprises people
Custom outputs do not bubble
A native DOM event such as click bubbles up through every ancestor element. An Angular output does not. Only the component that placed the child in its template can listen, and only to that child's output. If a grandparent needs the event, the parent must handle it and then emit an event of its own, or the two components can share state through a service.
Siblings do not talk directly
Two children of the same parent cannot bind to each other. The usual pattern is for one child to emit an event, for the parent to store the value in a signal, and for the parent to pass it down to the other child through an input. The parent is the meeting point.
Static attributes arrive as strings
Writing role="admin" without brackets sets the input to the literal string "admin". Writing [role]="admin" evaluates admin as an expression on the parent. If you write size="20" for an input meant to be a number, the child receives the string "20". The chapter on transforms shows how to convert such values.
Emitting is synchronous
When a child calls emit(), Angular runs the parent's handlers immediately, before emit() returns. If a handler throws, the error reaches the code that called emit(). Keep handlers short and let them update state, instead of doing slow work inline.
Inputs are not ready in the constructor
Angular sets input values after it creates the component. Reading this.step() in the constructor therefore returns the default, and for a required input it throws an error. Reading inputs inside a template, a computed(), or an effect() avoids the problem, because those run when the values are available. A later chapter covers timing in detail.
Where the rest of the tutorial goes
The remaining chapters take the pieces in this order:
- A refresher on signals, then
input()andinput.required(). - Computing values from inputs with
computed(). output(),emit(), and handling outputs in the parent.- Aliases and transforms, including
booleanAttributeandnumberAttribute. - Two-way binding with
model()and[(prop)]. - When input values become available, and RxJS interop with
outputFromObservable(). - Migrating from
@Inputand@Output, then common pitfalls and conventions.
Exercise
A Dashboard parent contains a Counter child. The child needs to show the parent's current theme name, and the parent needs to know every time the user clicks "reset" in the child. Which mechanism carries each piece of information, and what would you write in the parent's template to connect both?
Show answer
The theme name travels down, so it is an input on the child, for example theme = input('light'). The reset click travels up, so it is an output, for example reset = output<void>() with this.reset.emit() in the click handler. The parent template would be <app-counter [theme]="themeName()" (reset)="onReset()" />. The child never writes to theme. If the parent wants to change the theme after a reset, onReset() updates its own themeName signal.
Exercise
Two sibling components, FilterBar and ResultList, share a parent. FilterBar should change what ResultList shows. Describe how the data moves, without letting the siblings reference each other.
Show answer
FilterBar declares an output such as filterChanged = output<string>() and emits the new filter text. The parent handles (filterChanged) and stores the value in a signal, for example filter = signal(''). The parent then binds that signal into ResultList through an input: <app-result-list [filter]="filter()" />. The data goes up from one child to the parent and then down to the other child.