Sub topic 1
How parents and children communicate
Prerequisites
You should know TypeScript classes and generics, standalone Angular components, and Angular property and event binding. You should also know that a signal is read by calling it, such as count().
Parent-to-child communication
An Angular parent component passes data to a child through an input. The child declares the input, and the parent supplies a value with property binding:
<app-user-card [name]="user.name" />The child owns how the value is displayed, but the parent owns the value. This keeps data flow predictable: data moves down from parent to child.
A quick refresher on signals
A signal is a reactive value. Read it by calling it. Angular tracks that read and can update the template when the value changes.
computed() creates a read-only signal derived from other signals:
import { computed, signal } from '@angular/core';
name = signal('Ada Lovelace');
initials = computed(() => name().split(' ').map(part => part[0]).join(''));When name changes, initials() is recalculated when needed. An input created with input() is also read as a signal.
Declaring inputs with input()
Use input() as a class field. Its value is available in the template by calling the field:
import { Component, input } from '@angular/core';
@Component({
selector: 'app-user-card',
template: `
<h2>{{ name() }}</h2>
<p>Role: {{ role() }}</p>
`,
})
export class UserCard {
name = input<string>();
role = input('guest');
}name is an optional input whose type includes undefined until a parent supplies it. role has the default value 'guest'. The parent can set either input:
<app-user-card [name]="user.name" role="admin" />The static role attribute supplies a string. Use property binding when the value comes from an expression.
Making an input required
Use input.required<T>() when the child cannot work without a value:
name = input.required<string>();Angular then reports a missing binding at compile time when possible. A required input still remains read-only inside the child. The parent changes it by passing a new value.
Exercise
How should a child read an input named title?
Show answer
Declare it with title = input<string>(); and read it as title() in the class or template.