Reading Route Parameters
Route parameters are dynamic segments in the URL. Angular provides multiple ways to read them.
Route Definition with Parameters
export const routes: Routes = [
{ path: 'products/:id', component: ProductDetailComponent },
{ path: 'users/:userId/posts/:postId', component: PostComponent },
];
Reading Params with ActivatedRoute
import { Component, inject } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
@Component({
selector: 'app-product-detail',
template: `<p>Product ID: {{ productId }}</p>`,
})
export class ProductDetailComponent {
private route = inject(ActivatedRoute);
productId = '';
constructor() {
// Snapshot — one-time read (does not react to changes)
this.productId = this.route.snapshot.paramMap.get('id') || '';
}
}
Observable-Based Param Reading
@Component({
selector: 'app-product-detail',
template: `<p>Product ID: {{ productId }}</p>`,
})
export class ProductDetailComponent implements OnInit {
private route = inject(ActivatedRoute);
productId = '';
ngOnInit(): void {
// Observable — reacts to param changes
this.route.paramMap.subscribe(params => {
this.productId = params.get('id') || '';
});
}
}
Snapshot vs Observable
| Approach | Use When |
|---|---|
snapshot.paramMap |
Component is not reused (navigating away and back) |
paramMap.subscribe() |
Component may be reused (e.g., /products/1 -> /products/2) |
Query Parameters
// URL: /products?category=electronics&page=2
this.route.queryParamMap.subscribe(params => {
const category = params.get('category'); // 'electronics'
const page = params.get('page'); // '2'
});
Fragment
// URL: /products#reviews
this.route.fragment.subscribe(fragment => {
console.log('Fragment:', fragment); // 'reviews'
});
Multiple Parameters and Data Resolvers
Routes can have multiple parameters and static data.
Multiple Parameters
// Route definition
{ path: 'users/:userId/posts/:postId', component: PostComponent }
// URL: /users/42/posts/7
this.route.paramMap.subscribe(params => {
const userId = params.get('userId'); // '42'
const postId = params.get('postId'); // '7'
});
Route Data Property
Static data can be attached to routes:
export const routes: Routes = [
{
path: 'about',
component: AboutComponent,
data: { title: 'About Us', showHeader: true },
},
];
this.route.data.subscribe(data => {
console.log(data['title']); // 'About Us'
console.log(data['showHeader']); // true
});
ParamMap API
const paramMap = this.route.snapshot.paramMap;
paramMap.get('id'); // string | null
paramMap.getAll('id'); // string[]
paramMap.has('id'); // boolean
paramMap.keys; // string[]
Complete Example
@Component({
selector: 'app-product-detail',
template: `
<h1>Product {{ productId }}</h1>
<p>Category: {{ category }}</p>
<p>Page: {{ page }}</p>
`,
})
export class ProductDetailComponent implements OnInit {
private route = inject(ActivatedRoute);
productId = '';
category = '';
page = '1';
ngOnInit(): void {
// Route params
this.route.paramMap.subscribe(params => {
this.productId = params.get('id') || '';
});
// Query params
this.route.queryParamMap.subscribe(params => {
this.category = params.get('category') || 'all';
this.page = params.get('page') || '1';
});
}
}
Quiz
1. What is the difference between snapshot and observable param reading?
2. How do you read query parameters?
3. When should you use observable-based param reading instead of snapshot?
Flashcards
Question
How do you read route parameters?
Click to reveal answer
Answer
route.snapshot.paramMap.get('id') for one-time, or route.paramMap.subscribe() for reactive.
Question
How do you read query parameters?
Click to reveal answer
Answer
route.queryParamMap.subscribe(params => params.get('key'))
Question
When to use observable vs snapshot?
Click to reveal answer
Answer
Observable when component may be reused. Snapshot when navigating away and back.
Revision Notes
Key Takeaways
- 1. paramMap reads dynamic route segments
- 2. queryParamMap reads query string parameters
- 3. Use observable subscription for reusable components
- 4. Snapshot is fine for one-time reads
Interview Tips
- • Explain when to use snapshot vs observable
- • Be ready to describe ParamMap API
- • Mention query params for filters and pagination
Cheat Sheet
Route Params Cheat Sheet
Route definition:
{ path: 'products/:id', component: ProductDetailComponent }
Snapshot (one-time):
this.route.snapshot.paramMap.get('id')
Observable (reactive):
this.route.paramMap.subscribe(params => params.get('id'))
Query params:
this.route.queryParamMap.subscribe(params => params.get('category'))