Explain it simply
Route Parameters, in plain language
You can access URL parameters to build dynamic routes.
Problem → Why → Solution
The problem — why this concept matters
Reading only a snapshot can leave stale data when the ID changes while the component is reused.
A common mistake
One happy-path result does not prove the feature works; empty, loading, error, and cleanup behavior need explicit tests.
Core concept — understand how it works
Route parameters should represent resource identity, query parameters optional filters, sorting, or pagination, and fragments document locations. Because a component may be reused, subscribe to parameter changes or use route-input binding when a one-time snapshot is insufficient. Treat URL text as untrusted input and parse, validate, handle not-found, and cancel stale requests.
- URL or navigation
- router config and guard
- component view
Explain the core concept and Angular data flow of Route Parameters
Core syntax or pattern
// app-routing.module.ts
const routes: Routes = [
{ path: 'user/:id', component: UserDetailComponent },
{ path: 'product/:id/:name', component: ProductComponent }
];
// user-detail.component.ts
import { ActivatedRoute } from '@angular/router';
export class UserDetailComponent implements OnInit {
userId: string;
constructor(private route: ActivatedRoute) {}
ngOnInit() {
// Method 1: Snapshot (one-time read)
this.userId = this.route.snapshot.paramMap.get('id');
// Method 2: Observable (reactive, updates when params change)
this.route.paramMap.subscribe(params => {
this.userId = params.get('id');
this.loadUser(this.userId);
});
}
}
// Navigate programmatically
constructor(private router: Router) {}
goToUser(id: number) {
this.router.navigate(['/user', id]);
}Route parameters should represent resource identity, query parameters optional filters, sorting, or pagination, and fragments document locations. Because a component may be reused, subscribe to parameter changes or use route-input binding when a one-time snapshot is insufficient. Treat URL text as untrusted input and parse, validate, handle not-found, and cancel stale requests.
Read the code step by step
- URL or navigation
// app-routing.module.tsYou can access URL parameters to build dynamic routes.
- router config and guard
this.userId = this.route.snapshot.paramMap.get('id');Route parameters should represent resource identity, query parameters optional filters, sorting, or pagination, and fragments document locations. Because a component may be reused, subscribe to parameter changes or use route-input binding when a one-time snapshot is insufficient. Treat URL text as untrusted input and parse, validate, handle not-found, and cancel stale requests.
- component view
}Build products/:id with ID validation, switched requests on parameter change, a not-found state, and a query-synchronized tab.
- URL or navigation
- router config and guard
- component view
Success
Explain the core concept and Angular data flow of Route Parameters
Failure
Reading only a snapshot can leave stale data when the ID changes while the component is reused.
You can access URL parameters to build dynamic routes.
// app-routing.module.ts
const routes: Routes = [
{ path: 'user/:id', component: UserDetailComponent },
{ path: 'product/:id/:name', component: ProductComponent }
];
// user-detail.component.ts
import { ActivatedRoute } from '@angular/router';
export class UserDetailComponent implements OnInit {
userId: string;
constructor(private route: ActivatedRoute) {}
ngOnInit() {
// Method 1: Snapshot (one-time read)
this.userId = this.route.snapshot.paramMap.get('id');
// Method 2: Observable (reactive, updates when params change)
this.route.paramMap.subscribe(params => {
this.userId = params.get('id');
this.loadUser(this.userId);
});
}
}
// Navigate programmatically
constructor(private router: Router) {}
goToUser(id: number) {
this.router.navigate(['/user', id]);
}(You can read parameters from the URL and use them inside the component)Real-world usage
How to apply it in a real project
Build products/:id with ID validation, switched requests on parameter change, a not-found state, and a query-synchronized tab.
Mini challenge
Build it yourself
Rebuild the Route Parameters example without copying it. Add two normal states, one edge case, and one failure state, and write the expected UI and state transition before running it.
- Rebuild the example without copying it
- Test a normal input and an edge case
- Predict the compiler or output result before running it
// app-routing.module.ts
const routes: Routes = [
{ path: 'user/:id', component: UserDetailComponent },
{ path: 'product/:id/:name', component: ProductComponent }
];
// user-detail.component.ts
import { ActivatedRoute } from '@angular/router';
export class UserDetailComponent implements OnInit {
userId: string;
constructor(private route: ActivatedRoute) {}
ngOnInit() {
// Method 1: Snapshot (one-time read)
this.userId = this.route.snapshot.paramMap.get('id');
// Method 2: Observable (reactive, updates when params change)
this.route.paramMap.subscribe(params => {
this.userId = params.get('id');
this.loadUser(this.userId);
});
}
}
// Navigate programmatically
constructor(private router: Router) {}
goToUser(id: number) {
this.router.navigate(['/user', id]);
}Expected: Explain the core concept and Angular data flow of Route Parameters
Show hint
Build products/:id with ID validation, switched requests on parameter change, a not-found state, and a query-synchronized tab.
Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- URL or navigation → router config and guard → component view
- Route parameters should represent resource identity, query parameters optional filters, sorting, or pagination, and fragments document locations. Because a component may be reused, subscribe to parameter changes or use route-input binding when a one-time snapshot is insufficient. Treat URL text as untrusted input and parse, validate, handle not-found, and cancel stale requests.
- Reading only a snapshot can leave stale data when the ID changes while the component is reused.
- Build products/:id with ID validation, switched requests on parameter change, a not-found state, and a query-synchronized tab.
Angular — Routing — Angular