Sub topic 3
Declaring inputs with input()
This chapter covers the input() function, which declares a signal-based input on an Angular component. It assumes you know TypeScript classes and generics, standalone components and template syntax, property binding with [prop], and the basics of signals (you read a signal by calling it).
What input() gives you
An input is a property that a parent component can set from its template. Before signals, you declared inputs with the @Input() decorator. The input() function does the same job but returns a read-only signal. Because the value is a signal, Angular tracks where you read it and updates those places when the parent passes a new value.
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');
}Two things to notice:
nameis created withinput.required<string>(). The parent must supply it, and the type isstring.roleis created withinput('guest'). The argument is the default value, and TypeScript infers the typestringfrom it.
Both are read in the template by calling them: name() and role().
Passing values from the parent
The parent sets inputs in its template. Use property binding for expressions and a plain attribute for fixed strings.
<app-user-card [name]="user.name" role="admin" />Here [name] binds an expression from the parent's class. role="admin" passes the literal string "admin". If the parent omits role, the child sees 'guest'.
Optional and required inputs
An optional input always has a value, because you provide a default. If you give no default, the value is undefined, and the type includes it.
nickname = input<string>(); // InputSignal<string | undefined>
role = input('guest'); // InputSignal<string>A required input has no default. If a parent uses the component and leaves out a required input, the compiler reports an error. This moves a runtime mistake to build time.
Deriving values with computed()
Inputs are signals, so you can build other signals from them. Use computed() to derive state. The derived value recalculates when the input changes.
import { Component, computed, input } from '@angular/core';
@Component({
selector: 'app-avatar',
template: `<span class="avatar">{{ initials() }}</span>`,
})
export class Avatar {
name = input.required<string>();
initials = computed(() =>
this.name()
.split(' ')
.map((part) => part[0])
.join('')
);
}When the parent passes "Ada Lovelace", initials() is "AL". When the parent later passes "Grace Hopper", initials() becomes "GH" with no extra code. Prefer computed() over copying an input into a separate field, because a copy goes stale when the input changes.
Inputs are read-only
An input signal has no set() or update() method. The parent owns the value. If the child needs to change something and tell the parent, it uses an output (covered in a later chapter) or a model().
When the value is available
Input values are not set when the constructor runs. Angular sets them after it creates the component. For a required input, reading it in the constructor throws an error. Read inputs inside templates, computed(), or effect(), where Angular reads them at a safe time.
Common errors
| Symptom | Cause | Fix |
|---|---|---|
| Compiler error about a missing input | A required input has no binding | Bind it in the parent template |
| Runtime error reading an input in the constructor | The value is not set yet | Read it in a template or `computed()` |
| Template shows a function instead of text | You wrote `{{ name }}` | Call it: `{{ name() }}` |
| Derived value never changes | You copied the input once | Use `computed()` |
Exercise
A component declares size = input(16). A parent uses <app-box size="20" />. What is the type of size() inside the child, and what value does it hold?
Show answer
The type is number, inferred from the default 16, but the value is the string "20". A plain attribute passes a string, and TypeScript's type check does not stop it in this form. A later chapter shows how to convert it with a transform such as numberAttribute. To pass a real number now, use [size]="20".