Fix a shipment board that stopped updating after going zoneless
Solution idea
Report 1 comes from the zoneless switch itself. refresh() writes syncStatus after an await; zone.js used to run change detection when the timer fired, but without it nothing tells Angular that a plain field changed, so the view keeps Syncing…. Keeping syncStatus in a signal fixes it: set() schedules the refresh.
Solution
Overview
Report 1 comes from the zoneless switch itself. refresh() writes syncStatus after an await; zone.js used to run change detection when the timer fired, but without it nothing tells Angular that a plain field changed, so the view keeps Syncing…. Keeping syncStatus in a signal fixes it: set() schedules the refresh.
Report 2 is an in-place change. addShipment() pushes into the array the signal already holds and returns that same array, so the signal sees no new value: count does not recompute and the OnPush list receives the same input. Return a new array, [...list, shipment].
Report 3 is OnPush reading state nobody tells it about. The list compares selection.selectedId in its template, a plain field on a shared service, and Next is clicked outside the list, so nothing marks the list for check while the parent, which runs default change detection, shows the new id. Make the selection a signal in SelectionService, expose it read-only, and read selectedId() in both templates. Turning OnPush off or putting zone.js back would hide reports 1 and 3 without fixing the reads.
Files
<!doctype html><html><head><meta charset="utf-8"><title>UF</title><base href="/"/></head><body><app-root></app-root></body></html>import 'zone.js';
import { provideZonelessChangeDetection } from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app/app.component';
// The board moved to zoneless change detection in the last release.
bootstrapApplication(AppComponent, {
providers: [provideZonelessChangeDetection()]
}).catch(console.error);
:host {
display: block;
min-height: 100vh;
padding: 24px;
background: #0a0a0a;
color: #e5e7eb;
font-family: system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, sans-serif;
}
.board {
max-width: 520px;
margin: 0 auto;
display: grid;
gap: 12px;
}
.toolbar {
display: flex;
align-items: baseline;
justify-content: space-between;
}
.title {
margin: 0;
font-size: 20px;
}
.count,
.sync,
.selected {
color: #9ca3af;
font-size: 14px;
}
.sync,
.selected {
margin: 0;
}
.actions {
display: flex;
gap: 8px;
}
.actions button {
padding: 8px 12px;
border: 1px solid #374151;
border-radius: 8px;
background: #111827;
color: #e5e7eb;
cursor: pointer;
}
<main class="board">
<header class="toolbar">
<h1 class="title">Shipments</h1>
<span class="count" data-testid="shipment-count">{{ count() }} shipments</span>
</header>
<p class="sync" data-testid="sync-status">{{ syncStatus() }}</p>
<div class="actions">
<button type="button" data-testid="refresh" (click)="refresh()">Refresh</button>
<button type="button" data-testid="add-shipment" (click)="addShipment()">Add shipment</button>
<button type="button" data-testid="select-next" (click)="selectNext()">Next</button>
</div>
<p class="selected" data-testid="selected-shipment">Selected: {{ selection.selectedId() }}</p>
<app-shipment-list [shipments]="shipments()" />
</main>
import { Component, computed, inject, signal } from '@angular/core';
import { SelectionService } from './selection.service';
import { ShipmentListComponent } from './shipment-list.component';
import { INITIAL_SHIPMENTS, syncWithCarrier } from './shipments';
@Component({
selector: 'app-root',
imports: [ShipmentListComponent],
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
protected readonly selection = inject(SelectionService);
readonly shipments = signal(INITIAL_SHIPMENTS);
readonly count = computed(() => this.shipments().length);
// Written after an await. zone.js used to run change detection after the timer; without it only a signal write
// schedules a refresh.
readonly syncStatus = signal('Not synced yet');
private nextNumber = 104;
async refresh(): Promise<void> {
this.syncStatus.set('Syncing…');
const time = await syncWithCarrier();
this.syncStatus.set(`Synced at ${time}`);
}
addShipment(): void {
// A new array: the signal compares references, so a push into the old one would change nothing it can see.
this.shipments.update((list) => [
...list,
{ id: `S-${this.nextNumber++}`, route: 'Berlin → Hamburg', status: 'In transit' }
]);
}
selectNext(): void {
this.selection.selectNext(this.shipments().map((shipment) => shipment.id));
}
}
import { Injectable, signal } from '@angular/core';
@Injectable({ providedIn: 'root' })
export class SelectionService {
// A signal tells every view that reads it, OnPush views included, that the selection moved.
private readonly selected = signal('S-101');
readonly selectedId = this.selected.asReadonly();
selectNext(ids: string[]): void {
const index = ids.indexOf(this.selected());
this.selected.set(ids[(index + 1) % ids.length]);
}
}
.list {
margin: 0;
padding: 0;
list-style: none;
display: grid;
gap: 6px;
}
.row {
display: grid;
grid-template-columns: 64px 1fr auto;
gap: 8px;
padding: 10px 12px;
border: 1px solid #1f2937;
border-radius: 8px;
}
.row.is-selected {
border-color: #60a5fa;
background: #0b1a33;
}
.id {
font-weight: 700;
}
.status {
color: #9ca3af;
}
import { ChangeDetectionStrategy, Component, inject, input } from '@angular/core';
import { SelectionService } from './selection.service';
@Component({
selector: 'app-shipment-list',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<ul class="list">
@for (shipment of shipments(); track shipment.id) {
<li
class="row"
data-testid="shipment-row"
[class.is-selected]="selection.selectedId() === shipment.id"
[attr.aria-selected]="selection.selectedId() === shipment.id"
>
<span class="id">{{ shipment.id }}</span>
<span class="route">{{ shipment.route }}</span>
<span class="status">{{ shipment.status }}</span>
</li>
}
</ul>
`,
styleUrls: ['./shipment-list.component.css']
})
export class ShipmentListComponent {
readonly shipments = input.required<{ id: string; route: string; status: string }[]>();
protected readonly selection = inject(SelectionService);
}
export const INITIAL_SHIPMENTS = [
{ id: 'S-101', route: 'Lisbon → Porto', status: 'In transit' },
{ id: 'S-102', route: 'Madrid → Valencia', status: 'Delayed' },
{ id: 'S-103', route: 'Paris → Lyon', status: 'Delivered' }
];
// Stands in for the carrier API: answers after 300 ms with the time of the sync.
export function syncWithCarrier(): Promise<string> {
return new Promise((resolve) => setTimeout(() => resolve(new Date().toLocaleTimeString()), 300));
}
Similar questions
Guides
Preparing for interviews? Use Frontend Coding Challenges first, then move into a concrete Study Plan before targeted Company Prep.
Built in public
The code behind FrontendAtlas lives on GitHub. A star helps other frontend engineers find it.