Route Parameters, Query Params and Component Input Binding
Route Parameters
typescript
export const routes: Routes = [
{ path: 'products', component: ProductList },
{ path: 'products/:id', component: ProductDetail }, // :id is a parameter
];
URL /products/42 shows ProductDetail with id = 42.
Easiest Way: Component Input Binding
Turn on one router feature and route values arrive as inputs:
typescript
// app.config.ts
provideRouter(routes, withComponentInputBinding())
typescript
@Component({
selector: 'app-product-detail',
template: `
<h2>Product #{{ id() }}</h2>
@if (product(); as p) { <p>{{ p.name }} - {{ p.price | currency }}</p> }
`,
})
export class ProductDetail {
id = input.required<string>(); // from /products/:id
q = input<string>(); // from ?q=... (query param)
private http = inject(HttpClient);
product = toSignal(
toObservable(this.id).pipe(switchMap(id => this.http.get<Product>(`/api/products/${id}`))));
}
Convert types: Route values are strings. Use
input(0, { transform: numberAttribute }) to get a number.
The Classic Way: ActivatedRoute
typescript
export class ProductDetail implements OnInit {
private route = inject(ActivatedRoute);
ngOnInit() {
// snapshot: read once
const id = this.route.snapshot.paramMap.get('id');
// observable: updates when only the id changes (same component reused)
this.route.paramMap.subscribe(params => console.log(params.get('id')));
}
}
Important: Going from
/products/1 to /products/2 reuses the same component. A snapshot would show old data. Use inputs (signals) or the observable.
Query Parameters and Fragments
html
<a routerLink="/products" [queryParams]="{ category: 'books', page: 2 }">Books page 2</a>
<a routerLink="/docs" fragment="install">Jump to Install</a>
<!-- URL: /products?category=books&page=2 -->
typescript
export class ProductList {
private route = inject(ActivatedRoute);
private router = inject(Router);
query = toSignal(this.route.queryParamMap, { initialValue: convertToParamMap({}) });
category = computed(() => this.query().get('category') ?? 'all');
page = computed(() => Number(this.query().get('page') ?? 1));
nextPage() {
this.router.navigate([], {
queryParams: { page: this.page() + 1 },
queryParamsHandling: 'merge', // keep other params
});
}
}
| Type | URL | Use for |
|---|---|---|
| Path parameter | /products/42 | Identify a resource |
| Query parameter | /products?page=2&sort=price | Filters, paging, search (optional) |
| Fragment | /docs#install | Scroll to a section |
Keep UI state in the URL: Storing the page and filters in query parameters lets users share links and refresh the page without losing their place.
Conclusion
Use :id in the path, enable withComponentInputBinding() and receive route and query values as inputs. Use queryParamsHandling: 'merge' when changing one query value.
Quick Summary: path :id + withComponentInputBinding() = inputs from the URL.

