Sub topic 11
Signal-Based Inputs, Outputs and Models
This chapter assumes you have read the earlier chapters on @Input and @Output and on using signals in templates. You should be comfortable with signal(), computed(), and calling a signal in a template with count(). The examples need Angular 17.3 or later. These APIs became stable in Angular 19, so use a current version if you can.
Decorator-based inputs and outputs still work. Angular also has three functions that do the same jobs with signals:
input()declares a value the parent passes in. You read it as a read-only signal.output()declares an event the child sends to the parent.model()declares a writable signal that is an input and an output at once. It is what makes two-way binding work.
Why signal-based inputs exist
An @Input property is a plain field. Angular writes to it, and the component cannot tell when that happens unless it adds ngOnChanges or a setter. Values derived from inputs end up spread across lifecycle hooks.
A signal input is a signal, so everything that works with signals works with it too. You can derive values with computed(), run side effects with effect(), and read the input in the template. Change detection can then track exactly which views depend on it.
Declaring inputs with input()
input() returns an InputSignal<T>. The argument you pass is the default value.
import { Component, computed, input } from '@angular/core';
@Component({
selector: 'app-price-tag',
template: `
<span class="name">{{ product() }}</span>
<span class="price">{{ formatted() }}</span>
`,
})
export class PriceTagComponent {
product = input.required<string>();
amount = input(0);
currency = input<'USD' | 'EUR'>('USD');
formatted = computed(() =>
new Intl.NumberFormat('en-US', {
style: 'currency',
currency: this.currency(),
}).format(this.amount())
);
}The parent binds to it exactly as it would bind to a decorator input:
import { Component } from '@angular/core';
import { PriceTagComponent } from './price-tag.component';
@Component({
selector: 'app-root',
imports: [PriceTagComponent],
template: `
<app-price-tag product="Keyboard" [amount]="49.5" />
<app-price-tag product="Mouse" [amount]="19" currency="EUR" />
`,
})
export class AppComponent {}Some details matter here:
- The type comes from the default value.
input(0)isInputSignal<number>. Withinput<string>()and no default, the type isstring | undefined. input.required<T>()has no default. If a parent leaves the input out, the compiler reports an error. The type is justT, so you don't needundefinedchecks.formattedupdates by itself wheneveramountorcurrencychanges. You don't needngOnChanges.
Aliases and transforms
input() takes an options object. alias sets the public binding name. transform converts the incoming value before the signal stores it.
import { Component, booleanAttribute, input, numberAttribute } from '@angular/core';
@Component({
selector: 'app-panel',
template: `
@if (!collapsed()) {
<section [style.max-height.px]="maxHeight()">
<ng-content />
</section>
}
`,
})
export class PanelComponent {
collapsed = input(false, { transform: booleanAttribute });
maxHeight = input(400, { alias: 'height', transform: numberAttribute });
}With booleanAttribute, you can write <app-panel collapsed>. The empty string becomes true. With numberAttribute, height="250" becomes the number 250. A transform must be a pure function. Angular calls it every time the bound value changes.
Signal inputs are read-only
The child component can't write to an InputSignal. It has no set() or update() method. This rule is deliberate: the parent owns the value. With @Input, you could reassign the field inside the child. The next time the parent's binding changed, Angular would overwrite your value, and that caused confusing bugs.
If the child needs local state that starts from an input, keep it in a separate signal. Use linkedSignal() (Angular 19+) when that state should reset whenever the input changes:
import { Component, input, linkedSignal } from '@angular/core';
@Component({
selector: 'app-page-picker',
template: `
<button (click)="page.set(page() - 1)" [disabled]="page() <= 1">Prev</button>
Page {{ page() }}
<button (click)="page.set(page() + 1)">Next</button>
`,
})
export class PagePickerComponent {
startPage = input(1);
page = linkedSignal(() => this.startPage());
}If the parent must see the change too, you want model() instead. It is covered below.
Declaring outputs with output()
output<T>() returns an OutputEmitterRef<T>. It has one method that matters in components: emit(value).
import { Component, input, output } from '@angular/core';
export interface RatingChange {
id: number;
stars: number;
}
@Component({
selector: 'app-star-rating',
template: `
@for (star of [1, 2, 3, 4, 5]; track star) {
<button (click)="rated.emit({ id: itemId(), stars: star })">★{{ star }}</button>
}
<button (click)="cleared.emit()">Clear</button>
`,
})
export class StarRatingComponent {
itemId = input.required<number>();
rated = output<RatingChange>();
cleared = output<void>();
submitted = output<number>({ alias: 'ratingSubmitted' });
}The parent listens with ordinary event binding: <app-star-rating [itemId]="3" (rated)="save($event)" (cleared)="reset()" />. $event has the type RatingChange.
Compared with EventEmitter, output() is not an RxJS Subject. It has no pipe(), next() or error(). You lose nothing in templates, and the component no longer needs RxJS just to emit events. If you already have an Observable, convert it with outputFromObservable() from @angular/core/rxjs-interop:
import { Component } from '@angular/core';
import { outputFromObservable } from '@angular/core/rxjs-interop';
import { interval, map } from 'rxjs';
@Component({
selector: 'app-ticker',
template: `<p>Ticking…</p>`,
})
export class TickerComponent {
tick = outputFromObservable(interval(1000).pipe(map(n => n + 1)));
}Angular cleans up the subscription when the component is destroyed.
Two-way state with model()
model() declares a writable signal that Angular exposes as a pair of bindings. If the field is named count, Angular creates an input named count and an output named countChange. When the child calls set() or update(), the new value is emitted on countChange. The parent's [(count)] binding then writes it back.
import { Component, model, signal } from '@angular/core';
@Component({
selector: 'app-volume',
template: `
<button (click)="level.update(v => Math.max(0, v - 10))">−</button>
<span>{{ level() }}%</span>
<button (click)="level.update(v => Math.min(100, v + 10))">+</button>
`,
})
export class VolumeComponent {
level = model(50);
}
@Component({
selector: 'app-root',
imports: [VolumeComponent],
template: `
<app-volume [(level)]="volume" />
<p>Parent volume: {{ volume() }}</p>
<button (click)="volume.set(0)">Mute</button>
`,
})
export class AppComponent {
volume = signal(30);
}The two directions work like this:
- From parent to child: the parent calls
volume.set(0). Angular passes0intolevel, and the child shows0%. - From child to parent: the child updates
level. Angular emitslevelChange, and the parent'svolumesignal changes.
Binding a signal or a plain field
In the banana-in-a-box binding, pass the signal itself, not its value: [(level)]="volume". Angular sees a WritableSignal and calls set() on it. Writing [(level)]="volume()" is a compile error, because a function call can't be the target of an assignment.
A plain field also works: [(level)]="volumeField". Angular assigns to the field.
One-way bindings to a model
A model() also works with one-way bindings. [level]="volume()" passes the value in, but the parent never learns about changes the child makes. The two copies then drift apart until the parent's value changes again and overwrites the child's. If you see a value "snap back", look for a one-way binding to a model. A parent can also listen without binding in: (levelChange)="log($event)".
model.required<T>() works like input.required. It has no default, and the parent must bind it.
Decorators versus signal functions
| Concern | `@Input` / `@Output` | `input()` / `output()` / `model()` |
|---|---|---|
| Reading the value | Plain field `this.id` | Call the signal `this.id()` |
| Reacting to changes | `ngOnChanges` or a setter | `computed()` / `effect()` |
| Required values | `@Input({ required: true })`, field still needs `!` | `input.required<T>()`, type is `T` |
| Child writes to input | Allowed, then overwritten | Not allowed. Use `model()` or local state |
| Event type | `EventEmitter` (an RxJS Subject) | `OutputEmitterRef`, no RxJS needed |
| Two-way binding | `@Input() x` + `@Output() xChange` by hand | `model()` |
| Setting inputs in tests | `componentRef.setInput()` | `componentRef.setInput()` (direct assignment fails) |
The testing row matters during migration. A test that does component.label = 'X' no longer compiles against an InputSignal. Use fixture.componentRef.setInput('label', 'X'). It works with both styles and runs transforms. The Angular CLI can migrate code automatically with ng generate @angular/core:signal-input-migration and ng generate @angular/core:output-migration. Review the result, because the migration skips inputs that the component writes to itself.
Common errors
- Forgetting the parentheses.
{{ label }}in the template prints the signal's function source instead of its value. Use{{ label() }}. - Calling the signal in a two-way binding.
[(count)]="clicks()"fails. Use[(count)]="clicks". - Declaring an input with
constor in a method.input(),output()andmodel()must be called in a class field initializer. The compiler reports an error anywhere else. - Expecting
output()to carry RxJS operators. UseoutputToObservable()when a consumer needs a stream.
Exercise
A ToggleComponent declares checked = input(false), and its button handler calls this.checked.set(!this.checked()). The code does not compile. Rewrite the field so the parent can use <app-toggle [(checked)]="darkMode" />, where darkMode = signal(false), and name the output Angular generates.
Show answer
Change the field to checked = model(false);. An InputSignal has no set(), but a ModelSignal does. The handler can then call this.checked.set(!this.checked()) or this.checked.update(v => !v). Angular generates an input named checked and an output named checkedChange. The [(checked)] binding listens to checkedChange and calls darkMode.set(...).
Exercise
A child has size = input.required<number>() and a constructor that runs console.log(this.size()). What happens at runtime, and how do you fix it while still logging every new value?
Show answer
Angular throws NG0950, because the constructor runs before inputs are set. Move the read into a reactive context. For example, add constructor() { effect(() => console.log(this.size())); }. The effect first runs after the input has its value, and it runs again whenever size changes.
Exercise
A parent writes <app-volume [level]="volume()" /> with the VolumeComponent from this chapter. The user clicks + twice, and then the parent calls volume.set(30) while volume is already 30. What does each component show, and why?
Show answer
The child shows 70% and the parent still shows 30. The one-way binding never sends levelChange back to the parent. Calling volume.set(30) with the same value does not notify, because signals skip equal values. The binding's input does not change, so the child keeps 70. Use [(level)]="volume" to keep the two in sync.