Sub topic 3
Using Signals in Templates
This chapter assumes you already know property binding, event binding, @Input/@Output, and the [( )] two-way syntax from earlier chapters.
Reading Signals in Templates
A signal is a value wrapper that tells Angular when it changes. In a template, you read a signal by calling it, as in {{ count() }}. Angular records which signals each template reads. When one of them changes, Angular marks that component for update.
A common error is writing {{ count }} without the parentheses. That renders the signal function itself instead of its value.
Signal-Based Inputs, Outputs and Models
The input(), output() and model() functions replace the decorators:
| API | Role | Read or write |
|---|---|---|
| `input()` / `input.required()` | Data from the parent | Read-only signal |
| `output()` | Event to the parent | `.emit(value)` |
| `model()` | Two-way value | Writable signal |
Building Components That Support Two-Way Binding
A model() named count creates a count input and a countChange output for you. Because of this, the parent can bind to it with [(count)]:
import { Component, input, model, output, signal } from '@angular/core';
@Component({
selector: 'app-counter',
template: `
<p>{{ label() }}: {{ count() }}</p>
<button (click)="increment()">+</button>
<button (click)="reset.emit()">Reset</button>
`,
})
export class CounterComponent {
label = input.required<string>();
count = model(0);
reset = output<void>();
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);
}When the child calls count.update(), the parent's clicks signal updates too. The parent passes the signal itself ([(count)]="clicks"), not clicks().
How Change Detection Updates Bindings
Change detection is the process in which Angular re-evaluates template bindings and patches the DOM wherever a value differs.
Signals tell Angular exactly which components need checking. Plain fields only get picked up when change detection checks the whole component tree. That full check is triggered by Zone.js, or by an event inside the component when it uses OnPush.
Sanitization and Safe Binding Practices
Angular treats every bound value as untrusted. Interpolation always inserts text, never markup. Bindings such as [innerHTML], [href] and [style] go through sanitization, which strips <script> tags, javascript: URLs and other unsafe content.
Exercise
A child component declares value = model(''). What does the parent write to keep its text = signal('') in sync, and which output does Angular generate?
Show answer
The parent writes <app-child [(value)]="text" />. Angular generates a valueChange output.
Exercise
Why does {{ count }} not show the number when count is a signal?
Show answer
A signal is a function, and you read its value by calling it. The template needs {{ count() }}.