Sub topic 11
Signal-Based Inputs, Outputs and Models
This chapter assumes you know the decorator-based @Input() and @Output() pattern from the previous chapters. It also assumes you can read a signal in a template by calling it, as in {{ count() }}. Angular has three functions that do the same jobs as the decorators but use signals: input(), output() and model().
Why Replace the Decorators
An @Input() property is a plain field, so Angular cannot tell when the value inside it changes. Code that depends on that value needs ngOnChanges or a setter. An input() is a read-only signal. You can pass it to computed() or effect() like any other signal, and Angular tracks which templates read it.
| Job | Decorator API | Signal API | How you read or send |
|---|---|---|---|
| Receive data | `@Input() id = 0` | `id = input(0)` | `this.id()` |
| Required data | `@Input({ required: true })` | `input.required<number>()` | `this.id()` |
| Notify parent | `@Output() remove = new EventEmitter<number>()` | `remove = output<number>()` | `this.remove.emit(7)` |
| Two-way value | `@Input()` plus `@Output() xChange` | `x = model(0)` | `this.x()`, `this.x.set(...)` |
The Three Functions Together
import { Component, computed, input, model, output, signal } from '@angular/core';
@Component({
selector: 'app-counter',
template: `
<p>{{ label() }}: {{ count() }} (double: {{ doubled() }})</p>
<button (click)="increment()">+</button>
<button (click)="reset.emit()">Reset</button>
`,
})
export class CounterComponent {
label = input.required<string>();
count = model(0);
reset = output<void>();
doubled = computed(() => this.count() * 2);
increment() {
this.count.update(c => c + 1);
}
}
@Component({
selector: 'app-root',
imports: [CounterComponent],
template: `
<app-counter label="Clicks" [(count)]="clicks" (reset)="clicks.set(0)" />
<p>Parent sees {{ clicks() }}</p>
`,
})
export class AppComponent {
clicks = signal(0);
}The parent template uses each API in its own way:
label="Clicks"sets a required input to a static string. Leaving it out is a compile-time error.[(count)]="clicks"connects the parent'sclickssignal to the child'scountmodel. When the child callscount.update(...), the parent's signal changes as well.(reset)="clicks.set(0)"listens for the output in the same way it would listen for a DOM event.
How model() Works
model(0) creates a writable signal. It also registers an input named count and an output named countChange. When the child writes to the model, Angular emits countChange with the new value. The [(count)] syntax expands to [count]="clicks" (countChange)="clicks.set($event)". If the parent binds a signal, Angular updates that signal directly. If the parent binds a plain field, Angular assigns to the field instead.
Use input() when the child only reads the value. Use model() when the child is also allowed to change it.
Input Options
input() takes an options object. alias changes the public binding name. transform converts the incoming value before the signal stores it:
import { Component, booleanAttribute, input } from '@angular/core';
@Component({
selector: 'app-panel',
template: `<section [class.compact]="compact()">Panel</section>`,
})
export class PanelComponent {
compact = input(false, { transform: booleanAttribute });
}With this transform, <app-panel compact /> sets compact() to true, even though the attribute value is an empty string.
Common Errors
- Writing to an input. An
InputSignalhas nosetmethod, sothis.label.set('x')does not compile. If the child needs to change the value, make it amodel(). - Forgetting the parentheses.
{{ label }}prints the signal function itself, not its value. Write{{ label() }}. - Reading required inputs too early. If you read
input.required()in the constructor, Angular throws an error (NG0950) because no value has been bound yet. Read it in the template, incomputed(), or inngOnInitor later. - Expecting RxJS from output().
output()returns anOutputEmitterRef, not anObservable. To expose an existing stream as an output, useoutputFromObservable()from@angular/core/rxjs-interop.
Exercise
A child component declares value = input(0) and calls this.value.set(5) when a button is clicked. What goes wrong, and what change lets the parent's [(value)]="score" binding receive the new number?
Show answer
input() returns a read-only InputSignal with no set method, so the code does not compile. Declare value = model(0). Then this.value.set(5) updates the child's signal and emits valueChange, which the [(value)] binding uses to update score.
Exercise
Write the signal-based declarations for a component that requires a userId number, accepts an optional title string defaulting to 'Untitled', and emits a saved event that carries a string.
Show answer
userId = input.required<number>();, title = input('Untitled'); and saved = output<string>();, which you trigger with this.saved.emit('some text').