Sub topic 1
How parents and children communicate
Prerequisites
This chapter assumes you know:
- TypeScript classes and generics.
- Angular standalone components and template syntax.
- Property binding
[prop]and event binding(event)in templates. - The basics of Angular signals, where you read a signal by calling it.
The two directions of data
An Angular app is a tree of components. A parent component places a child component in its template. The two need to share information, and Angular defines exactly two channels for it:
- Data flows down from parent to child through inputs.
- Events flow up from child to parent through outputs.
A child never reaches into its parent to change something. It announces that something happened, and the parent decides what to do. This keeps each component easy to reason about, because a child's data comes from one place: whoever uses it.
Inputs and outputs in the modern API
Angular provides two functions for these channels:
input()declares a value the parent can pass in. The result is a read-only signal.output()declares an event the child can emit. The parent listens to it.
Both are class fields initialized in the component, and both are typed. Here is a child that receives data:
import { Component, input } from '@angular/core';
@Component({
selector: 'app-user-card',
template: `
<h2>{{ name() }}</h2>
<p>Role: {{ role() }}</p>
`,
})
export class UserCard {
name = input.required<string>();
role = input('guest');
}name is required and has type string. role is optional and defaults to 'guest'. Because an input is a signal, the template reads it by calling it: name().
The parent supplies the values in its own template:
<app-user-card [name]="user.name" role="admin" />[name]="user.name" is a property binding, so the child receives whatever user.name evaluates to and updates when it changes. role="admin" is a static attribute, so the child receives the fixed string 'admin'.
Events going back up
A child reports back by emitting an output. This counter takes an input and emits an output:
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);
}
}output<number>() creates an event whose payload is a number. Calling emit() sends that payload to the parent. The parent listens with ordinary event binding:
<app-counter [step]="5" (changed)="onChanged($event)" />Inside the handler expression, $event holds the value passed to emit(). Here it is the new count, typed as number.
One component, both channels
app-counter shows the full loop in a single component:
- The parent passes
stepdown with[step]="5". - The user clicks the button and the child updates its own
count. - The child emits
changedwith the new count. - The parent's
onChanged($event)runs and can store or react to the value.
How this differs from the older API
Older Angular code used the @Input() and @Output() decorators. They do the same job, but an @Input() is a plain property, not a signal. The function-based API gives you three things:
- Inputs are signals, so you can derive values from them and react to changes.
- Required inputs are checked when the template is compiled.
- Types flow through, including the type of
$event.
Later chapters cover each of these and show how to migrate old components.
Exercise
In the app-counter example, which direction does each of step and changed send information, and how does the parent connect to each one in its template?
Show answer
step is an input, so data flows from parent to child. The parent connects with property binding, [step]="5". changed is an output, so events flow from child to parent. The parent connects with event binding, (changed)="onChanged($event)", where $event is the emitted count.