Sub topic 2
Conditionals and Lists with @if, @for, and @switch
This chapter assumes you know HTML, TypeScript classes, and the earlier material on interpolation, signals, property binding, and event binding.
Control flow
Angular templates have built-in blocks for logic. @if renders a block when its condition is true, and @else covers the other case. @for repeats a block once per item. It requires a track expression, which gives each item a stable identity so Angular can reuse DOM nodes when the list changes. @empty renders when the list is empty. @switch picks one @case block, or falls back to @default.
A common error is to write track $index for lists that get reordered. That ties each DOM node to a position instead of an item, so reordering re-renders every row. Track a unique id instead.
Inputs and outputs
input() declares a value that the parent passes down. input.required<T>() makes Angular report a compile error if the parent leaves it out. output<T>() declares an event that the child emits upward. In older code you will see @Input() and @Output() x = new EventEmitter(), which do the same job using decorators. Remember to call input signals as functions (title()) when you read them.
import { Component, input, output, signal } from '@angular/core';
@Component({
selector: 'app-item',
template: `
<label [class.done]="done()" [attr.aria-checked]="done()" [style.opacity]="done() ? 0.5 : 1">
<input type="checkbox" [checked]="done()" (change)="toggled.emit(!done())" />
{{ title() }}
</label>
`,
styles: `.done { text-decoration: line-through; }`
})
export class ItemComponent {
title = input.required<string>();
done = input(false);
toggled = output<boolean>();
}
@Component({
selector: 'app-list',
imports: [ItemComponent],
template: `
@for (t of todos(); track t.id) {
<app-item [title]="t.title" [done]="t.done" (toggled)="set(t.id, $event)" />
} @empty {
<p>Nothing to do.</p>
}
@switch (todos().length) {
@case (0) { <p>Empty list</p> }
@default { <p>{{ todos().length }} items</p> }
}
`
})
export class ListComponent {
todos = signal([{ id: 1, title: 'Learn @for', done: false }]);
set(id: number, done: boolean) {
this.todos.update(l => l.map(t => (t.id === id ? { ...t, done } : t)));
}
}Two-way binding with model()
model() creates an input that the child can also write to. The parent binds it with [(on)]="mySignal". This is shorthand for [on] combined with (onChange). The child updates it with on.set(value).
Attribute, class, and style bindings
| Syntax | Sets |
|---|---|
| `[attr.aria-label]="x"` | An HTML attribute that has no DOM property |
| `[class.done]="flag"` | Adds or removes one CSS class |
| `[style.width.px]="n"` | One style property, with an optional unit |
Content projection
<ng-content /> inserts the markup the parent writes between the component's tags. <ng-content select="[card-title]" /> creates a named slot that receives only the elements matching that selector. Content that matches no slot goes to the default <ng-content />.
Exercise
A child declares value = model(0). Write the parent binding that keeps the parent's count signal in sync with it, and explain what it expands to.
Show answer
<app-child [(value)]="count" />. It expands to [value]="count()" plus (valueChange)="count.set($event)". Angular wires the signal directly, so the parent and the child stay in sync in both directions.