← Back to Angular, page 9

angular-0164 · Easy fresher

What does the input transform option do, and how do booleanAttribute and numberAttribute help with HTML-style attributes?

HTML attributes are strings, so <app-tab disabled> passes an empty string and <app-box size="3"> passes "3". An input transform converts the incoming value before the component sees it. booleanAttribute and numberAttribute are the built-in transforms, and you can write your own for trimming or clamping.

Full answer

When you write a plain attribute in a template, such as <app-chip disabled> or <app-box size="3">, Angular passes a string. For disabled, the value is the empty string '', which is falsy, so a naive check treats it as "not disabled". This is the problem that input transforms solve.

A transform is a function that runs when the parent sets the input. The component receives the converted value, and the type of the input is the type after conversion. You write it as input(false, { transform: booleanAttribute }).

Built-in transforms:

  • booleanAttribute: '' becomes true, 'true' becomes true, 'false' becomes false, undefined and null become false, and any other string becomes true. So <app-chip disabled>, <app-chip disabled="true"> and [disabled]="flag" all work.
  • numberAttribute: converts strings to numbers, so size="3" becomes 3. It returns NaN for text that is not a number (you can give a fallback: numberAttribute(value, 10) through a small wrapper).

Custom transforms are plain functions: trim a string, clamp a number into a range (rating between 0 and 5), or turn a comma separated string into an array. Keep them pure, fast and without side effects, because they run on every input change. They should accept the wider type the parent may pass (string | number) and return the type the component wants.

The same option exists for decorator inputs: @Input({ transform: booleanAttribute }) disabled = false;.

Why it matters: it makes your component feel like a native HTML element, and removes if (typeof x === 'string') checks inside the component. With strictTemplates, the template type is the input's accepted type, so [size]="'abc'" is still accepted as a string and becomes NaN at run time. Validate where it matters.

Do not use a transform for logic that depends on other inputs. Use computed for that.

typescript
import { Component, booleanAttribute, input, numberAttribute } from '@angular/core';

function clamp(value: number | string | undefined): number {
  const n = numberAttribute(value, 0);
  return Math.min(5, Math.max(0, n));
}

@Component({
  selector: 'app-rating-chip',
  template: `<span>{{ label() }} | disabled={{ disabled() }} | size={{ size() }} | rating={{ rating() }}</span>`,
})
export class RatingChipComponent {
  readonly label = input('Chip');
  readonly disabled = input(false, { transform: booleanAttribute });
  readonly size = input(1, { transform: numberAttribute });
  readonly rating = input(0, { transform: clamp });
}

@Component({
  selector: 'app-chip-demo',
  imports: [RatingChipComponent],
  template: `
    <app-rating-chip id="a" disabled size="3" rating="9" />
    <app-rating-chip id="b" disabled="false" rating="2.5" />
    <app-rating-chip id="c" [disabled]="off" />
  `,
})
export class ChipDemoComponent {
  off = true;
}

Angular 20.3, strict template check passes. A Karma test renders the component with a bare attribute, with `"false"`, with a number string and with an out of range rating, and checks the resulting text.

Key points

  • Plain attributes pass strings, so `<x disabled>` gives `''`.
  • `transform` converts the value before the component sees it.
  • `booleanAttribute` and `numberAttribute` are built in. Custom transforms are pure functions.
  • Use `computed` for logic that depends on several inputs.

Common mistakes

  • Checking `if (this.disabled)` with a raw string input and treating `''` as false.
  • Writing transforms with side effects.
  • Expecting `numberAttribute` to throw on text. It gives `NaN`.

Follow-ups

  • How would you clamp a rating between 0 and 5?
  • What is the type of the input after the transform?
  • When would you use `computed` and not a transform?

Tips

  • Start with the `disabled` attribute problem.
  • Show both built-ins and one custom transform.
  • Say that transforms must be pure.

Evaluation rubric

  • Explains the string attribute problem.
  • Uses the built-in transforms correctly.
  • Writes and limits custom transforms.