Sub topic 3
Declaring inputs with input()
An input is a property of a child component that a parent can set from its template. This chapter covers the input() function. It covers how to declare an input, how to read it, how typing and default values work, and what the compiler requires of the declaration. It assumes you know TypeScript classes and generics, standalone components, property and event binding, and that you read a signal by calling it.
What input() gives you
input() creates a signal-based input. You call it once, as a class field initializer, and it returns an InputSignal<T>. This is a read-only signal. Angular writes new values into it when the parent's bound value changes. Your component can only read it.
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');
}Both fields are signals, so the template reads them with name() and role(). The template re-renders whenever the parent supplies a new value.
The parent sets the inputs through the component's selector:
<app-user-card [name]="user.name" role="admin" />[name]="user.name" is a property binding. The expression is evaluated, and the result goes to the input every time it changes. role="admin" is a static attribute. Angular passes the string "admin" once.
Types and default values
The arguments to input() decide the type of the signal. There are three common forms.
| Declaration | Type of the signal | Value when the parent sets nothing | Column |
|---|---|---|---|
| `input('guest')` | `InputSignal<string>` | `'guest'` | |
| `input<string>()` | `InputSignal<string \ | undefined>` | `undefined` |
| `input<string>('guest')` | `InputSignal<string>` | `'guest'` |
With no default, the type automatically includes undefined, because the parent is allowed to leave the input unset. Reading it forces you to handle that case:
import { Component, input } from '@angular/core';
type Size = 'small' | 'medium' | 'large';
@Component({
selector: 'app-badge',
template: `<span [class]="'badge ' + size()">{{ label() }}</span>`,
})
export class Badge {
label = input<string>();
size = input<Size>('medium');
}Here label() can be undefined, and interpolation renders that as an empty string. The size input needs the explicit type argument. Without <Size>, TypeScript would infer string from 'medium', and the parent could pass any string.
With strict template checking, a parent that writes size="huge" gets a compile-time error, because "huge" is not assignable to Size.
<app-badge label="New" size="large" />Choosing a default
A default should be a value that makes the component work correctly if the parent says nothing. Use input<T>() with an undefined default only when "absent" is a meaningful state that your component handles. If the component cannot work without the value, a default is wrong. The next chapter covers required inputs for that case.
Reading inputs
Because an input is a signal, you read it by calling it. That works anywhere a signal can be read.
- In a template,
{{ size() }}and[class]="size()"track the signal automatically. - In
computed(), the computed value is recalculated when the input changes. The chapter on computing values from inputs covers this. - In
effect(), the effect reruns when the input changes. - In an ordinary method, such as an event handler, you get the current value at that moment, with no tracking.
import { Component, effect, input } from '@angular/core';
@Component({
selector: 'app-logger-card',
template: `<p>{{ title() }}</p>`,
})
export class LoggerCard {
title = input('Untitled');
constructor() {
effect(() => {
console.log('title is now', this.title());
});
}
}The effect logs once after the first render with the initial value. It logs again each time the parent passes a different title.
Inputs are read-only
An InputSignal has no set() or update() method. Calling either is a compile error. Assigning to the field replaces the signal itself, which breaks the link to the parent.
// Both lines are mistakes inside a component that declares name = input('');
this.name.set('Ada'); // Error: Property 'set' does not exist
this.name = 'Ada'; // Error: string is not assignable to InputSignal<string>This restriction is deliberate. Data flows one way, from parent to child. If the child needs a value it can change locally, copy the input into its own state. The usual tool is linkedSignal(), or a plain signal() if you only need the initial value. If the child needs to change the value and tell the parent, it should use model(), which a later chapter covers.
Where input() may be called
Angular's compiler finds inputs by scanning for input() calls in class field initializers. Because of that, input() has two requirements:
- It must be a class field initializer on a component or directive. Calling it inside a method or the constructor body does not register an input.
- The field should not be
privateor#private. The template and the parent's binding need access to it, so usepublic, which is the default, orprotectedif only the template reads it.
import { Component, input } from '@angular/core';
@Component({
selector: 'app-bad',
template: ``,
})
export class Bad {
title!: ReturnType<typeof input<string>>;
constructor() {
// Wrong: the compiler does not see this as an input declaration,
// and input() also needs an injection context to run.
this.title = input<string>();
}
}Declare the field directly instead: title = input<string>();.
Binding forms and what they pass
How the parent writes the binding determines the value type. This catches many intermediate developers out.
| Parent template | Value the child receives |
|---|---|
| `<app-user-card name="Ada" />` | The string `"Ada"` |
| `<app-user-card [name]="'Ada'" />` | The string `"Ada"` |
| `<app-user-card [name]="userName" />` | The current value of `userName` |
| `<app-user-card [name]="userName()" />` | The current value of the `userName` signal |
| `<app-user-card name />` | The empty string `""` |
A static attribute is always a string. If the input is input(0) and the parent writes count="5", strict template checking reports a type error, because "5" is not a number. To pass a number, write [count]="5". If you want attributes like count="5" to work, the input needs a transform, which a later chapter covers.
A few more points about bindings:
- Pass the signal's value, not the signal. Writing
[name]="userName"whereuserNameis a signal passes the signal object. Write[name]="userName()". - Angular compares the new value with the old one using
Object.is. If a parent mutates an object in place and passes the same reference, the input signal does not notify. Pass a new object to trigger updates. - Binding to a name that is not an input fails to compile with an error such as "Can't bind to 'name' since it isn't a known property". A static attribute with an unknown name compiles, but it becomes a plain HTML attribute on the host element, not an input.
Inputs and change detection
Signal inputs work well with OnPush components. When the parent's bound value changes, Angular writes the new value into the input signal. Any template or computed value that reads the signal is marked for update. The component does not need ChangeDetectorRef calls to refresh. Reading the signal in the template is enough.
During construction, the parent has not yet written any values. Calling this.size() in the constructor returns the default, not what the parent passed. Inputs are set after the component is created, before its template first renders. A later chapter covers this timing in detail.
Summary of the declaration rules
- Call
input()as a public or protected class field initializer. - Pass a default to get a non-nullable type, or a type argument alone to get
T | undefined. - Give an explicit type argument when you want a union or a wider type than the default infers.
- Read it by calling it, and never write to it.
- Bind with
[prop]for non-string values, and use a plain attribute only for string values.
Exercise
A component declares count = input(0); and a parent template writes <app-stepper count="5" />. What goes wrong, and how do you fix it in the parent?
Show answer
A static attribute always passes a string, so "5" is not assignable to the number type of count. Strict template checking reports an error. Fix it in the parent with a property binding, <app-stepper [count]="5" />, which passes the number 5.
Exercise
What is the type of level = input<'low' | 'high'>();, and what does level() return when the parent does not bind it?
Show answer
The type is InputSignal<'low' | 'high' | undefined>. With no default argument, the value is undefined until the parent sets it, so level() returns undefined.
Exercise
A developer writes this.title.set('New title') inside a component that declares title = input('');. Why does it fail, and what are two valid alternatives?
Show answer
InputSignal is read-only and has no set() method, because data flows from parent to child. One alternative is to copy the value into local state with linkedSignal(() => this.title()), which the component can change itself. The other is to use model('') instead of input(''), so the component can update the value and notify the parent.