Sub topic 13
Migrating from @Input and @Output
This chapter assumes you know the signal-based input(), input.required(), output() and model() functions from the earlier chapters. It also assumes you can read a decorator-based component. It covers how to convert existing code, what the Angular schematics do for you, and which patterns need manual work.
Why migrate
The decorator API still works, and Angular has not removed it. Migrating gives you these benefits:
- Type safety. A required input declared with
@Input({ required: true }) name!: stringis stillundefinedwhen read too early.input.required<string>()throws a clear error in that case instead. - Reactivity. A signal input works with
computed()andeffect(). A decorated field needsngOnChangesor a setter to react to changes. - Read-only inputs. A signal input has no
set()method, so a child cannot overwrite what its parent passed. - Simpler outputs.
output()replacesEventEmitter, which carries RxJS baggage you rarely need.
You can migrate one component at a time. Parents bind to signal inputs with the same [name] and (event) syntax, so a parent never needs to know which style a child uses.
The mapping at a glance
| Decorator style | Function style |
|---|---|
| `@Input() size = 16;` | `size = input(16);` |
| `@Input({ required: true }) name!: string;` | `name = input.required<string>();` |
| `@Input({ alias: 'label' }) text = '';` | `text = input('', { alias: 'label' });` |
| `@Input({ transform: booleanAttribute }) disabled = false;` | `disabled = input(false, { transform: booleanAttribute });` |
| `@Output() changed = new EventEmitter<number>();` | `changed = output<number>();` |
| `@Input() value` plus `@Output() valueChange` | `value = model<T>()` |
Reads change too. Every this.size in the class becomes this.size(), and every {{ size }} in the template becomes {{ size() }}. This is the most common source of compile errors after a migration, and the compiler reports each one.
A full before and after
Here is a rating component written with decorators. It copies an input into a derived field inside ngOnChanges.
import {
Component,
EventEmitter,
Input,
OnChanges,
Output,
numberAttribute,
} from '@angular/core';
@Component({
selector: 'app-rating',
template: `
@for (filled of stars; track $index) {
<button (click)="pick($index + 1)">{{ filled ? '★' : '☆' }}</button>
}
`,
})
export class Rating implements OnChanges {
@Input({ required: true, transform: numberAttribute }) value!: number;
@Input({ transform: numberAttribute }) max = 5;
@Output() rated = new EventEmitter<number>();
stars: boolean[] = [];
ngOnChanges() {
this.stars = Array.from({ length: this.max }, (_, i) => i < this.value);
}
pick(n: number) {
this.rated.emit(n);
}
}The migrated version replaces the lifecycle hook and the mutable stars field with a computed().
import { Component, computed, input, numberAttribute, output } from '@angular/core';
@Component({
selector: 'app-rating',
template: `
@for (filled of stars(); track $index) {
<button (click)="pick($index + 1)">{{ filled ? '★' : '☆' }}</button>
}
`,
})
export class Rating {
value = input.required<number, unknown>({ transform: numberAttribute });
max = input(5, { transform: numberAttribute });
rated = output<number>();
stars = computed(() =>
Array.from({ length: this.max() }, (_, i) => i < this.value())
);
pick(n: number) {
this.rated.emit(n);
}
}Four things changed:
ngOnChangesandimplements OnChangesare gone.starsrecomputes whenvalueormaxchanges, and only when something reads it.- The non-null assertion
!is gone.input.requiredneeds no initial value. - The transform changed the generics.
input.required<number, unknown>names the stored type first and the accepted type second.input(5, { transform: numberAttribute })infers both types, because the initial value fixes the stored type. - The parent template is untouched:
<app-rating [value]="3" (rated)="save($event)" />works for both versions.
What the migration schematics do
Angular ships schematics that rewrite your code. Run them from the workspace root, and commit or stash your work first so you can review the diff.
ng generate @angular/core:signal-input-migration
ng generate @angular/core:output-migrationThe input schematic converts decorated inputs, rewrites reads in the class, and updates templates and host bindings that reference them. The output schematic converts EventEmitter fields into output() and updates the places where they are used. Both ask for a path, so you can migrate one folder at a time.
A schematic skips a member when it cannot prove the change is safe. Typical cases:
- The code writes to the input, such as
this.value = 10. - The input uses a setter or getter.
- The input is read inside a
ngOnChangesmethod that inspectsSimpleChanges. - The output is used with
.pipe()or other RxJS methods.
Skipped members stay as decorators. The schematics have a best-effort mode and an option to insert TODO comments at skipped members. Check ng generate @angular/core:signal-input-migration --help for the options in your Angular version.
Patterns that need manual work
Writes to an input
A decorated input is an ordinary field, so a component sometimes overwrites it, for example to hold a draft value. A signal input cannot be written. Pick one of these replacements:
- Use
model()if the parent should see the change. - Keep a separate local
signal()initialized from the input if the change is private. - Use
linkedSignal(() => this.value())if the local copy should reset whenever the input changes. This API is available in recent Angular versions, so check yours.
import { Component, input, linkedSignal } from '@angular/core';
@Component({
selector: 'app-name-editor',
template: `<input [value]="draft()" (input)="draft.set($any($event.target).value)" />`,
})
export class NameEditor {
name = input.required<string>();
draft = linkedSignal(() => this.name());
}Setters
A setter usually does one of two jobs. If it normalizes the value, move that logic into a transform. If it triggers a side effect, use effect().
import { Component, effect, input } from '@angular/core';
@Component({
selector: 'app-search',
template: `<p>Searching for {{ query() }}</p>`,
})
export class Search {
query = input('', { transform: (value: string | null) => value?.trim() ?? '' });
private logQuery = effect(() => {
console.log('query is now', this.query());
});
}Keep a transform pure and cheap. It runs whenever the parent binding changes, and it must not read other signals or call services.
ngOnChanges
Angular still calls ngOnChanges for signal inputs, so old code keeps working. New code should prefer computed() for derived values and effect() for side effects, because they track exactly the signals they read. ngOnChanges fires when any input changes, and it gives you no information about which signals your logic depends on.
Outputs used as observables
EventEmitter extends RxJS Subject, so code like this.rated.pipe(debounceTime(300)) compiles. OutputEmitterRef returns no observable. It has emit() and subscribe() only. If you need an RxJS stream, either keep the Subject private and expose it with outputFromObservable(), or convert an output with outputToObservable(). Both live in @angular/core/rxjs-interop.
Two-way pairs
If a component has @Input() checked and @Output() checkedChange, and the names follow the x and xChange convention, replace the pair with checked = model(false). Parents that use [(checked)] keep working. Parents that bind [checked] and (checkedChange) separately also keep working, because model() creates both.
Tests
Tests that assign component.value = 3 no longer compile. Use fixture.componentRef.setInput('value', 3), which works for both decorated and signal inputs. Then call fixture.detectChanges(). To check an output, call subscribe() on the OutputEmitterRef:
import { TestBed } from '@angular/core/testing';
import { Rating } from './rating';
describe('Rating', () => {
it('emits the picked star count', () => {
const fixture = TestBed.createComponent(Rating);
fixture.componentRef.setInput('value', 2);
fixture.detectChanges();
const emitted: number[] = [];
fixture.componentInstance.rated.subscribe((n) => emitted.push(n));
const buttons = fixture.nativeElement.querySelectorAll('button');
buttons[3].click();
expect(emitted).toEqual([4]);
});
});A migration checklist
The diagram shows the decision for each decorated member.
Work through a component in this order:
- Commit your current state.
- Run the schematics on one folder.
- Fix the skipped members by hand, using the cases above.
- Replace
ngOnChangeslogic withcomputed()oreffect(). - Update tests to use
setInput(). - Run the build and the test suite, then review the diff for templates that call inputs without
().
Exercise
A component has @Input() set pageSize(value: number) { this._size = Math.max(1, value); this.reload(); }. How would you migrate it to signal inputs? Explain which part goes where and why.
Show answer
Split the setter into its two jobs. The clamping goes into a transform: pageSize = input(10, { transform: (v: number) => Math.max(1, v) });. The call to reload() is a side effect, so it goes into an effect that reads the input: private reloadOnChange = effect(() => { this.pageSize(); this.reload(); });. If reload() itself reads or writes other signals, wrap it in untracked() so the effect depends only on pageSize. The transform stays pure, and the effect tracks pageSize, so it reruns whenever the parent passes a new value.