Sub topic 14
Pitfalls and conventions
This chapter assumes you know the basics from the earlier chapters: input(), input.required(), output(), computed(), and property and event binding in standalone components. It covers the mistakes that come up most often with signal-based inputs and outputs, and the naming conventions that avoid them.
Pitfall 1: writing to an input
An input is a read-only signal from the child's point of view. The parent owns the value. input() returns an InputSignal, which has no set() or update() method, so this fails to compile:
import { Component, input } from '@angular/core';
@Component({
selector: 'app-badge',
template: `<span>{{ label() }}</span>`,
})
export class Badge {
label = input('new');
reset() {
// Compile error: 'set' does not exist on InputSignal
// this.label.set('new');
}
}If the child must change the value, pick one of these options:
- Use
model()when the parent should see the change. - Keep a separate local
signal()when the change is private to the child. - Emit an
output()and let the parent update the value it passes down.
Pitfall 2: reading an input too early
Angular sets inputs after it creates the component. The constructor runs before that, so an optional input still holds its default and a required input has no value yet. Reading a required input at that point throws a runtime error (NG0950).
import { Component, effect, input } from '@angular/core';
@Component({
selector: 'app-greeting',
template: `<p>Hello, {{ name() }}</p>`,
})
export class Greeting {
name = input.required<string>();
constructor() {
// this.name(); // throws NG0950: input has no value yet
effect(() => console.log('Name is', this.name()));
}
}Use these places instead:
- Read the input in the template.
- Read it inside
computed(). - Read it inside
effect(), which runs after inputs are set. - Read it in
ngOnInit()or later.
Reads in computed() and effect() also re-run when the parent passes a new value. A one-time read in ngOnInit() does not.
Pitfall 3: the wrong binding form
A static attribute always passes a string. size="20" sends "20", not the number 20, unless the input has a numberAttribute transform. To pass a real value, use property binding:
<app-icon-button size="20" />
<app-icon-button [size]="20" />Forgetting the parentheses when reading an input is also common. {{ name }} prints the signal function rather than its value. Write {{ name() }}.
Pitfall 4: mutating object inputs
A signal input holds a reference. If the child changes a property on an object it received, the parent's object changes too, and the signal does not notify anyone because the reference is the same. Treat object and array inputs as immutable. To change one, emit an event with the new value and let the parent create a new object.
Conventions
| Topic | Convention |
|---|---|
| Required data | Use `input.required<T>()` instead of an optional input with a fake default. |
| Output names | Name them for what happened (`changed`, `selected`). Do not prefix them with `on`. |
| Native event names | Do not name an output `click`, `input`, or another DOM event. Parents' listeners may fire for both. |
| Declaration site | Call `input()`, `output()`, and `model()` only in class field initializers. |
| Derived values | Use `computed()` instead of copying an input into a field. |
| Aliases | Use them rarely. They make code harder to search for. |
Choosing the right tool
Exercise
A child component reads this.title() in its constructor, and title is declared with input.required<string>(). What happens, and how do you fix it?
Show answer
Angular has not set the input yet during construction, so reading it throws NG0950. Move the read into the template, a computed(), an effect(), or ngOnInit(). Use effect() or computed() if the code must react to later changes.
Exercise
A parent writes <app-icon-button size="20" />, and the child declares size = input(16) with no transform. What type does size() return, and what is the fix?
Show answer
It returns the string "20" at runtime, even though the type says number. The fix is to use [size]="20" or to declare input(16, { transform: numberAttribute }).