Sub topic 13
Migrating from @Input and @Output
This chapter assumes you know the input(), output() and model() functions from the earlier chapters. It also assumes you can read signals and use computed(). Here you will convert a component that uses the older decorators.
What changes and what stays the same
The parent template does not change. [name], (changed) and [(checked)] work the same against a decorator-based child or a signal-based child. You can migrate one component at a time without touching its users.
The changes are inside the component class:
| Decorator style | Signal style |
|---|---|
| `@Input() name = ''` | `name = input('')` |
| `@Input({ required: true }) name!: string` | `name = input.required<string>()` |
| `@Input({ alias: 'label' })` | `input('', { alias: 'label' })` |
| `@Input({ transform: numberAttribute })` | `input(0, { transform: numberAttribute })` |
| `@Output() changed = new EventEmitter<number>()` | `changed = output<number>()` |
| `@Input()` plus `@Output() xChange` | `x = model()` |
A worked migration
Here is a component written with decorators.
import { Component, EventEmitter, Input, Output } from '@angular/core';
@Component({
selector: 'app-counter',
template: `
<button (click)="increment()">+{{ step }}</button>
<span>{{ count }}</span>
`,
})
export class Counter {
@Input() step = 1;
@Output() changed = new EventEmitter<number>();
count = 0;
increment() {
this.count += this.step;
this.changed.emit(this.count);
}
}Here is the same component with signal-based functions.
import { Component, input, output } from '@angular/core';
@Component({
selector: 'app-counter',
template: `
<button (click)="increment()">+{{ step() }}</button>
<span>{{ count }}</span>
`,
})
export class Counter {
step = input(1);
changed = output<number>();
count = 0;
increment() {
this.count += this.step();
this.changed.emit(this.count);
}
}Every read of step becomes a call, step(), in both the template and the class. The compiler reports any read you miss, because a signal is not a number.
Replacing setters and ngOnChanges
Decorator inputs often react to changes through a setter or ngOnChanges. Signal inputs have neither. Replace them with these:
computed()when you derive a value from the input.effect()when a change must cause a side effect, such as logging or calling a service.
import { Component, computed, input } from '@angular/core';
@Component({
selector: 'app-greeting',
template: `<p>{{ shout() }}</p>`,
})
export class Greeting {
name = input.required<string>();
shout = computed(() => this.name().toUpperCase() + '!');
}This replaces a setter that stored an uppercased copy in a separate field. The derived value cannot go stale, because it depends on the signal.
Pairs that become model()
Look for an @Input() value next to an @Output() valueChange with the same base name. This pair is the two-way binding convention, and model() replaces both.
import { Component, model } from '@angular/core';
@Component({
selector: 'app-toggle',
template: `<button (click)="checked.set(!checked())">{{ checked() ? 'On' : 'Off' }}</button>`,
})
export class Toggle {
checked = model(false);
}Parents that wrote [(checked)]="isOn" keep working unchanged.
Using the CLI schematics
Angular ships automated migrations. Run them from the project root.
ng generate @angular/core:signal-input-migration
ng generate @angular/core:output-migrationEach schematic rewrites declarations and updates the reads in templates and classes. Run them on a clean Git branch and review the diff. Code the tool cannot convert safely is left as it was, usually because it writes to the input.
Differences in outputs
EventEmitter extended RxJS Subject, so code could call .pipe() or .subscribe() on it. The output() function returns an OutputEmitterRef. It offers emit() and subscribe() but not pipe(). If you relied on pipe(), convert with outputToObservable() from @angular/core/rxjs-interop.
Exercise
Exercise
A component has @Input() size = 12;, @Output() resized = new EventEmitter<number>();, and a method that runs this.resized.emit(this.size * 2). Write the signal-based declarations and the updated method body.
Show answer
Declarations: size = input(12); and resized = output<number>();. The method body becomes this.resized.emit(this.size() * 2). The read of size must be called as a function, and the parent template needs no change.