Sub topic 8
Aliasing inputs
This chapter assumes you can declare an input with input(), read it by calling it, and bind to it from a parent template. It covers the alias option, which lets a component use one name for an input internally and expose a different name to parents.
What an alias does
By default, the class property name is also the name parents bind to. The alias option separates the two.
import { Component, input } from '@angular/core';
@Component({
selector: 'app-icon-button',
template: `<button>{{ text() }}</button>`,
})
export class IconButton {
text = input('', { alias: 'label' });
}Inside the class and template, you read the input as text(). A parent sets it as label:
<app-icon-button label="Save" />
<app-icon-button [label]="buttonTitle" />Aliasing a required input
The options object works the same way with input.required(). Pass the type argument first, then the options.
import { Component, input } from '@angular/core';
@Component({
selector: 'app-tag',
template: `<span class="tag">{{ text() }}</span>`,
})
export class Tag {
text = input.required<string>({ alias: 'label' });
}The parent must now supply label. Omitting it is a compile-time error, just as it is for any required input.
The old name stops working
Once an alias is set, the property name is no longer part of the component's public API. This parent template fails to compile:
<app-tag [text]="'New'" />Angular reports that text is not a known property of app-tag. Only label is accepted.
When to use an alias
Aliases are useful in a few situations, and the Angular style guide recommends avoiding them otherwise.
| Situation | Why an alias helps |
|---|---|
| The natural name collides with a native attribute such as `title` or `hidden` | Binding to a native attribute can show a browser tooltip or hide the element in addition to setting your input. A different public name avoids that. |
| You want to rename the public API without touching internal code | Parents see the new name. Your class keeps its existing property name. |
| A directive's input should match its selector | Writing `[appHighlight]="'yellow'"` reads better than adding a second attribute. |
The third case looks like this:
import { Directive, input } from '@angular/core';
@Directive({
selector: '[appHighlight]',
host: { '[style.background]': 'color()' },
})
export class Highlight {
color = input('yellow', { alias: 'appHighlight' });
}<p appHighlight="lightblue">Highlighted text</p>The directive activates and receives its value through a single attribute.
Aliases and transforms
The alias and transform options can be combined in one options object.
import { Component, booleanAttribute, input } from '@angular/core';
@Component({
selector: 'app-switch',
template: `<button [disabled]="off()">Switch</button>`,
})
export class Switch {
off = input(false, { alias: 'disabled', transform: booleanAttribute });
}A parent can write <app-switch disabled />. The attribute is converted to true by booleanAttribute, then read internally as off().
Common mistakes
- Putting
aliasas a second argument instead of inside the options object. - Binding to the property name in a parent after adding an alias.
- Using an alias that is the same as another input's alias on the same component.
Exercise
A component declares size = input(16, { alias: 'iconSize' }). Which parent template is valid: <app-x [size]="24" /> or <app-x [iconSize]="24" />? How do you read the value inside the component class?
Show answer
<app-x [iconSize]="24" /> is valid, because parents use the alias. Inside the class, you read the value with this.size(), because the property name is unchanged.