Sub topic 9
Transforming input values
This chapter assumes you can declare inputs with input() and read signals by calling them. It shows how the transform option converts the value a parent passes before the component stores it.
Why transform an input
Values written as plain HTML attributes are always strings. In <app-icon-button size="20" disabled />, size is the string "20" and disabled is the empty string "". Without a transform, the component would have to handle those strings itself. With one, the component declares the type it wants and Angular converts the value on the way in.
Built-in transforms
Angular ships two transform functions for the common cases: numberAttribute and booleanAttribute.
import { Component, booleanAttribute, input, numberAttribute } from '@angular/core';
@Component({
selector: 'app-icon-button',
template: `
<button [disabled]="disabled()" [style.font-size.px]="size()">{{ text() }}</button>
`,
})
export class IconButton {
size = input(16, { transform: numberAttribute });
disabled = input(false, { transform: booleanAttribute });
text = input('', { alias: 'label' });
}A parent can now write this:
<app-icon-button size="20" disabled label="Save" />Inside the component, size() returns the number 20 and disabled() returns true. The alias option exposes the text field to parents as label. It combines freely with transform.
The table shows what each function does with typical values.
| Function | Input | Result |
|---|---|---|
| `numberAttribute` | `"20"` | `20` |
| `numberAttribute` | `"abc"` | `NaN` |
| `booleanAttribute` | `""` (bare attribute) | `true` |
| `booleanAttribute` | `"false"` | `false` |
| `booleanAttribute` | `undefined` or `null` | `false` |
Writing your own transform
A transform is any function that takes one argument and returns the value the input will hold. The input's type then has two parts. The first is the type you read inside the component. The second is the type a parent may pass. Angular infers both from the function signature.
import { Component, input } from '@angular/core';
function trimmed(value: string | null | undefined): string {
return value?.trim() ?? '';
}
function clamp(value: number | string): number {
const n = Number(value);
return Math.min(100, Math.max(0, Number.isNaN(n) ? 0 : n));
}
@Component({
selector: 'app-progress',
template: `<p>{{ title() }}: {{ percent() }}%</p>`,
})
export class Progress {
title = input('', { transform: trimmed });
percent = input(0, { transform: clamp });
}Here title accepts string | null | undefined from the parent, and title() always returns a string. Likewise, percent accepts a number or a string, and percent() always returns a number between 0 and 100.
Rules and gotchas
- The transform runs every time the parent supplies a new value, so keep it fast.
- Keep it pure. It should not read other inputs, change component state, or have side effects.
- Angular must be able to determine the accepted type at compile time. Use a named function or an imported one, and give its parameter an explicit type.
- A transform changes the type, not the validity.
numberAttribute("abc")givesNaN, so guard against it if that matters, asclampdoes above.
Transform or computed?
Use a transform when you want to normalize the incoming value itself, such as turning "20" into 20. Use computed() when you want to derive a different value from the input, such as initials from a name. A transform replaces the input's value. A computed() adds a new one beside it.
Exercise
A quantity input should accept 5 or "5" from a parent, and the component should always read a number. Write the input declaration.
Show answer
quantity = input(1, { transform: numberAttribute }); The default 1 sets the read type to number. numberAttribute accepts the string "5" or the number 5 and returns the number 5.
Exercise
A parent writes <app-icon-button disabled /> and another writes <app-icon-button disabled="false" />. What does disabled() return in each case with booleanAttribute?
Show answer
The first returns true, because a bare attribute is the empty string. The second returns false, because booleanAttribute treats the string "false" as false.