Sub topic 6
Signal Inputs with input()
This chapter assumes you can build Angular components with templates and property binding, and that you already know signal() and computed() from the earlier chapters. Here you will declare component inputs as signals, so that values passed in from a parent take part in the same reactive graph as the rest of your state.
From @Input() to input()
A decorator-based @Input() is a plain class property. Angular writes to it, and you find out about changes through ngOnChanges or a setter. A signal input is a read-only signal. Angular sets its value from the parent binding, and you read it by calling it. Because it is a signal, any computed() or effect() that reads it re-runs automatically when the parent passes a new value.
Declaring inputs
There are two forms of input():
| Form | Type of signal | Parent must bind it? | Column |
|---|---|---|---|
| `input(defaultValue)` | `InputSignal<T>` that starts at the default | No | |
| `input<T>()` | `InputSignal<T \ | undefined>` | No |
| `input.required<T>()` | `InputSignal<T>` with no undefined | Yes, compile error if missing |
Here is a price component that uses both forms and derives a formatted string:
import { Component, booleanAttribute, computed, input } from '@angular/core';
@Component({
selector: 'app-price',
template: `
<span [class.muted]="muted()">{{ formatted() }}</span>
`,
})
export class PriceComponent {
amount = input.required<number>();
currency = input('USD');
muted = input(false, { transform: booleanAttribute });
formatted = computed(() =>
new Intl.NumberFormat('en-US', {
style: 'currency',
currency: this.currency(),
}).format(this.amount())
);
}The parent binds to it exactly as it would to a decorator input:
<app-price [amount]="order.total" currency="EUR" muted />Passing muted with no value works because booleanAttribute converts the empty string attribute to true.
Options: transform and alias
The second argument to input() takes an options object.
transformconverts the raw bound value before the signal stores it. Angular shipsbooleanAttributeandnumberAttributefor common cases. The signal's type is the transform's return type.aliassets the public binding name when it should differ from the property name, for exampleinput(0, { alias: 'count' }). Use it sparingly, because it makes the component harder to search.
For input.required, options go in the first argument: input.required<number>({ alias: 'value' }).
Reacting to input changes
Do not reach for ngOnChanges. Express the dependency directly:
- Use
computed()for values derived from inputs, likeformattedabove. - Use
effect()for side effects that should follow an input, such as logging or calling a non-Angular widget.
Both track the input automatically, and they also track any other signals they read.
Common errors
Other mistakes come up often:
- Trying to write an input.
InputSignalhas noset()orupdate(). Inputs belong to the parent. If the child needs local state that starts from an input, uselinkedSignal()(covered later). If the child must write back to the parent, usemodel(). - Forgetting the parentheses.
{{ amount }}renders the signal function, not the number. Write{{ amount() }}. - Copying the value into a field.
total = this.amount()in a field initializer captures one value, or throws if the input is required. Usecomputed()instead. - Mutating an object input. If the parent mutates an object in place, the reference is the same, so the signal sees no change. Have the parent pass a new object.
Exercise
A component declares size = input.required<number>() and area = this.size() * this.size(); as a class field. What goes wrong, and how do you fix it?
Show answer
The field initializer runs during construction, before Angular sets the input, so reading size() throws NG0950. Even if it didn't throw, area would never update. Declare it as area = computed(() => this.size() * this.size());.
Exercise
You want <app-toggle disabled /> to disable the toggle without the parent writing [disabled]="true". How do you declare the input?
Show answer
disabled = input(false, { transform: booleanAttribute });. The attribute's empty string value is converted to true, and the signal is typed as boolean.