Sub topic 6
Reading Route Parameters
This chapter assumes you know standalone components, the inject() function, and basic RxJS Observables. It also assumes you can already define routes and link to them with RouterLink.
A route parameter is a named, variable segment of a URL path. In the route products/:id, the :id segment matches any value. For /products/42, the router captures id as '42'. The component needs that value to know which product to show.
Angular gives you two ways to read it: inject ActivatedRoute, or bind the parameter directly to a component input.
Reading Parameters with ActivatedRoute
ActivatedRoute is a service that describes the route currently rendered in a component's outlet. It has two views of the parameters:
snapshot.paramMapholds the values at the moment the component was created.paramMapis an Observable that emits again each time the parameters change.
import { Component, inject } from '@angular/core';
import { AsyncPipe } from '@angular/common';
import { ActivatedRoute } from '@angular/router';
import { map } from 'rxjs';
@Component({
selector: 'app-product-detail',
standalone: true,
imports: [AsyncPipe],
template: `
<h2>Product {{ id$ | async }}</h2>
<p>First loaded with id {{ initialId }}</p>
`
})
export class ProductDetailComponent {
private route = inject(ActivatedRoute);
readonly initialId = this.route.snapshot.paramMap.get('id');
readonly id$ = this.route.paramMap.pipe(map(params => params.get('id')));
}paramMap.get() returns string | null. It returns null if the route doesn't define that name.
Binding Parameters to Component Inputs
If you register the router with withComponentInputBinding(), the router sets component inputs whose names match route parameters. The same feature also binds query parameters and resolved data, which later chapters cover. Bound inputs update when the parameter changes, so component reuse doesn't cause stale values.
// main.ts
import { bootstrapApplication } from '@angular/platform-browser';
import { provideRouter, withComponentInputBinding } from '@angular/router';
import { AppComponent } from './app.component';
import { routes } from './app.routes';
bootstrapApplication(AppComponent, {
providers: [provideRouter(routes, withComponentInputBinding())]
});// product-detail.component.ts
import { Component, Input, numberAttribute } from '@angular/core';
@Component({
selector: 'app-product-detail',
standalone: true,
template: `<h2>Product {{ id }}</h2>`
})
export class ProductDetailComponent {
@Input({ transform: numberAttribute }) id!: number;
}Parameters always arrive as strings. The numberAttribute transform converts '42' to 42.
Choosing an Approach
| Approach | Updates on reuse | Router-independent component | Typical use |
|---|---|---|---|
| `snapshot.paramMap` | No | No | Components that are never reused with new params |
| `paramMap` Observable | Yes | No | Chaining into RxJS, such as `switchMap` to an HTTP call |
| Input binding | Yes | Yes | Most new standalone components |
Common Errors
- Name mismatch. With input binding, the input name must equal the parameter name exactly. If the route says
:productIdand the input is namedid, nothing gets bound, and you get no error. - Forgetting the feature. Without
withComponentInputBinding(), the inputs stayundefined. - Treating values as numbers.
params.get('id') === 42is always false because the value is a string. - Reading the parent's parameters. A child route's
ActivatedRoutecontains only its own segment's parameters. Useroute.parent?.paramMapfor the parent's, or enableparamsInheritanceStrategy: 'always'withwithRouterConfig.
Exercise
A product page has "Related product" links that point to /products/:id. Clicking one changes the URL, but the heading still shows the old product. The component reads this.route.snapshot.paramMap.get('id') in its constructor. Why does this happen, and what are two ways to fix it?
Show answer
The router reuses the existing ProductDetailComponent because only the parameter changed, so the constructor doesn't run again and the snapshot keeps the original value. One fix is to subscribe to this.route.paramMap, for example through the async pipe or switchMap. The other is to enable withComponentInputBinding() and declare an input named id, which the router updates on every parameter change.
Exercise
The route is { path: 'orders/:orderId', component: OrderComponent }, input binding is enabled, and OrderComponent declares @Input() id!: string. What value does id have at /orders/7?
Show answer
It is undefined. Input binding matches by name, and the parameter is orderId, not id. Rename the input to orderId, or rename the route parameter to :id.