Sub topic 10
Reacting to Route Parameters with ActivatedRoute
This chapter assumes you know how to configure Angular routes, use switchMap (covered earlier), and clean up subscriptions with takeUntilDestroyed or the async pipe.
Why route parameters are observables
ActivatedRoute describes the route that loaded a component. It exposes parameters in two forms:
snapshot.paramMapis the parameters at the moment the component was created.paramMapis an observable that emits a newParamMapevery time the parameters change.
The two forms matter because of component reuse. If the user navigates from /products/1 to /products/2, the router keeps the same ProductComponent instance because only the parameter changed. The constructor does not run again, so a value read once from the snapshot goes stale. The paramMap observable emits the new id, and your code can react to it.
Reading a parameter reactively
The simplest pattern maps the ParamMap to the value you need and subscribes. takeUntilDestroyed ends the subscription when the component is destroyed.
import { Component, inject } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { map } from 'rxjs';
@Component({
selector: 'app-product',
standalone: true,
template: `<p>Product {{ productId }}</p>`,
})
export class ProductComponent {
productId = '';
constructor() {
inject(ActivatedRoute)
.paramMap.pipe(
map(params => params.get('id') ?? ''),
takeUntilDestroyed()
)
.subscribe(id => (this.productId = id));
}
}params.get('id') returns string | null, so the ?? '' fallback keeps the type a plain string.
Loading data for each parameter
Usually the id is used to fetch something. Use switchMap here. When the user moves quickly from product 1 to product 2, switchMap cancels the request for product 1, so a slow response can't overwrite the newer one. Let the async pipe own the subscription.
import { Component, inject } from '@angular/core';
import { AsyncPipe } from '@angular/common';
import { ActivatedRoute } from '@angular/router';
import { HttpClient } from '@angular/common/http';
import { map, switchMap } from 'rxjs';
interface Product {
id: string;
name: string;
}
@Component({
selector: 'app-product-detail',
standalone: true,
imports: [AsyncPipe],
template: `
@if (product$ | async; as product) {
<h2>{{ product.name }}</h2>
}
`,
})
export class ProductDetailComponent {
private route = inject(ActivatedRoute);
private http = inject(HttpClient);
product$ = this.route.paramMap.pipe(
map(params => params.get('id') ?? ''),
switchMap(id => this.http.get<Product>(`/api/products/${id}`))
);
}Common mistakes
| Mistake | Effect | Fix |
|---|---|---|
| Reading `snapshot.paramMap` in a reusable component | Shows stale data after a same-route navigation | Use the `paramMap` observable |
| Using `mergeMap` for the fetch | An older, slower response can overwrite a newer one | Use `switchMap` |
| Subscribing inside another `subscribe` | Nested subscriptions leak and can't be cancelled | Flatten with `switchMap` |
| Using `params` instead of `paramMap` | Works, but you get a plain object with no `get`/`has` helpers | Prefer `paramMap` |
Exercise
A component reads this.route.snapshot.paramMap.get('id') in its constructor and loads an order. Navigating from /orders/5 to /orders/6 changes the URL, but the page still shows order 5. Why, and how do you fix it?
Show answer
The router reuses the component instance for the same route, so the constructor and the snapshot read do not run again. Replace the snapshot with this.route.paramMap.pipe(map(p => p.get('id') ?? ''), switchMap(id => this.http.get(/api/orders/${id}))) and render it with the async pipe, or subscribe with takeUntilDestroyed.
Exercise
Why is switchMap a better choice than concatMap for loading data from a route parameter?
Show answer
concatMap waits for each request to finish in order, so a fast user waits through outdated requests. switchMap cancels the in-flight request when a new id arrives, so only the latest product loads.