Full answer
Pagination needs three pieces of state: the full list, the page size, and the current page number. Everything else is derived:
pageCount = Math.max(1, Math.ceil(total / pageSize))rows = list.slice((page - 1) * pageSize, page * pageSize)canPrev = page > 1andcanNext = page < pageCount
Putting derived values in computed keeps them always correct. If the list shrinks (a filter or a delete), the current page may be beyond the last page, so clamp it. A neat way is a linkedSignal for the page, or to use Math.min(page, pageCount) in the computed that slices.
Edge cases to handle in the exercise:
- An empty list: show "No rows" and keep a page count of 1.
- A last page with fewer rows than the page size.
- Changing page size: go back to page 1, or the current page may not exist.
- Zero or negative page sizes: guard against them.
Server-side pagination is different. The list is not in memory. The server returns one page and a total count, and the page and size are query parameters. The same state model applies, but the rows come from a request (with switchMap or resource).
In the template use @for (row of rows(); track row.id) and [disabled] bound to the computed flags. For accessible tables, use <table>, <caption>, and <th scope="col">, and announce "Page 2 of 6".
Use this exercise to show that you separate state from derived data, and that you consider the edge cases without being asked.
import { Component, computed, linkedSignal, signal } from '@angular/core';
interface Row {
id: number;
name: string;
marks: number;
}
@Component({
selector: 'app-marks-pager',
template: `
<table>
<caption>Marks list</caption>
<thead>
<tr><th scope="col">Name</th><th scope="col">Marks</th></tr>
</thead>
<tbody>
@for (r of rows(); track r.id) {
<tr><td>{{ r.name }}</td><td>{{ r.marks }}</td></tr>
} @empty {
<tr><td colspan="2">No rows</td></tr>
}
</tbody>
</table>
<button (click)="prev()" [disabled]="!canPrev()">Previous</button>
<span aria-live="polite">Page {{ page() }} of {{ pageCount() }}</span>
<button (click)="next()" [disabled]="!canNext()">Next</button>
<label>
Rows per page
<select (change)="setSize($event)">
@for (n of [5, 10]; track n) {
<option [value]="n" [selected]="n === pageSize()">{{ n }}</option>
}
</select>
</label>
`,
})
export class MarksPagerComponent {
readonly list = signal<Row[]>(
Array.from({ length: 12 }, (_, i) => ({ id: i + 1, name: `Student ${i + 1}`, marks: 40 + ((i * 7) % 55) })),
);
readonly pageSize = signal(5);
readonly pageCount = computed(() => Math.max(1, Math.ceil(this.list().length / this.pageSize())));
// Resets to page 1 when the page size changes and never points past the last page.
readonly page = linkedSignal<{ size: number; count: number }, number>({
source: () => ({ size: this.pageSize(), count: this.pageCount() }),
computation: (src, previous) => {
if (!previous || previous.source.size !== src.size) {
return 1;
}
return Math.min(previous.value, src.count);
},
});
readonly rows = computed(() => {
const start = (this.page() - 1) * this.pageSize();
return this.list().slice(start, start + this.pageSize());
});
readonly canPrev = computed(() => this.page() > 1);
readonly canNext = computed(() => this.page() < this.pageCount());
prev(): void {
this.page.update((p) => p - 1);
}
next(): void {
this.page.update((p) => p + 1);
}
setSize(event: Event): void {
const size = Number((event.target as HTMLSelectElement).value);
if (size > 0) {
this.pageSize.set(size);
}
}
}Angular 20.3, strict template check passes. Set `pageSize` to see the last page with fewer rows.
Key points
- State: list, page size, page. Everything else is `computed`.
- Clamp the page when the list or size changes.
- Handle empty lists, partial last pages, and invalid sizes.
- Server-side paging uses the same model with a request per page.
Common mistakes
- Storing `rows` as a separate signal, which can go out of sync with the list.
- Not resetting to page 1 when the page size changes.
- Forgetting the `track` expression on the rows.
Follow-ups
- How would you add sorting without breaking pagination?
- How would you change this to server-side pagination?
- How would you keep the page in the URL?
Tips
- State the three pieces of state out loud before coding.
- Mention edge cases early. It is what separates a quick answer from a good one.
- Offer the server-side version as a follow-up.
Evaluation rubric
- Separates state from derived values.
- Handles clamping and edge cases.
- Uses control flow with `track` correctly.