Angular Route Parameters
Learn how to pass dynamic values through Angular routes and retrieve them inside components using ActivatedRoute.
What are Route Parameters?
Route parameters are dynamic values that can be included inside a route URL.
They are commonly used when the application needs to open a specific record, course, product or other resource.
For example, a route such as /courses/101 can use 101 as a course identifier.
Route Parameter Flow
Important Concepts
Dynamic Segment
A route can contain a dynamic segment such as
:id.
Route URL
The actual URL contains the value for the dynamic segment.
ActivatedRoute
Angular provides ActivatedRoute to access route information.
Parameter Value
The component can use the received value in its logic.
Defining a Route Parameter
A dynamic route parameter is defined using a colon followed by the parameter name.
In the following example, :id represents a dynamic route parameter.
const routes: Routes = [
{
path: 'courses/:id',
component: CourseDetailsComponent
}
];
Example URL
/courses/101
/courses/205
/courses/501
Route Parameter Example
Route: /courses/:id
Current URL: /courses/101
Parameter value: 101
Passing Parameter using routerLink
Route parameters can be passed from an Angular template using routerLink.
The parameter value is placed after the route path.
<a
[routerLink]="['/courses', 101]">
View Course
</a>
<a
[routerLink]="['/courses', course.id]">
View Course Details
</a>
Reading Route Parameters
Angular provides the ActivatedRoute service to read parameters from the current route.
The parameter can be accessed through paramMap.
import { Component } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
@Component({
selector: 'app-course-details',
templateUrl: './course-details.component.html'
})
export class CourseDetailsComponent {
courseId = '';
constructor(
private route: ActivatedRoute
) {
}
ngOnInit() {
this.courseId =
this.route.snapshot.paramMap.get('id') ?? '';
}
}
Using snapshot
The snapshot property provides the current route information at the time the component is created.
It can be used when the component does not need to react to future changes of the same route parameter.
const id =
this.route.snapshot.paramMap.get('id');
Using paramMap
paramMap provides access to route parameters using their parameter names.
For example, the id parameter can be accessed using get('id').
const courseId =
this.route.snapshot.paramMap.get('id');
console.log(courseId);
CIIT Training Institute Example
const routes: Routes = [
{
path: 'course/:id',
component: CourseDetailsComponent
}
];
<a
[routerLink]="['/course', 25]">
.NET Full Stack
</a>
<a
[routerLink]="['/course', 30]">
Java Full Stack
</a>
courseId = '';
constructor(
private route: ActivatedRoute
) {
}
ngOnInit() {
this.courseId =
this.route.snapshot.paramMap.get('id') ?? '';
}
CIIT Training Institute
Selected Course ID: 25
The same component can display different course details based on the route parameter.
Multiple Route Parameters
A route can contain more than one dynamic parameter.
Each parameter is defined using its own :parameterName syntax.
const routes: Routes = [
{
path: 'course/:courseId/topic/:topicId',
component: TopicComponent
}
];
/course/25/topic/4
const courseId =
this.route.snapshot.paramMap.get('courseId');
const topicId =
this.route.snapshot.paramMap.get('topicId');
Passing Parameters using navigate()
Parameters can also be supplied when performing programmatic navigation with navigate().
The parameter value is added as another route segment.
openCourse(courseId: number) {
this.router.navigate([
'/courses',
courseId
]);
}
Route Parameter vs Query Parameter
| Feature | Route Parameter | Query Parameter |
|---|---|---|
| Example |
/courses/101
|
/courses?id=101
|
| Definition | Defined inside route path |
Passed after ?
|
| Typical Use | Identify a specific resource | Filtering, sorting or optional information |
| Angular Access |
paramMap
|
queryParamMap
|
Important Points
A dynamic route parameter is defined using a colon, such as :id.
Use ActivatedRoute to access values from the current route.
Names such as courseId and topicId make the route easier to understand.
Use a meaningful URL structure when designing dynamic routes.
Route Parameter Best Practices
- Use descriptive names for route parameters.
- Use route parameters when a value identifies a specific resource.
-
Use
ActivatedRouteto access current route information. - Keep dynamic route structures simple and meaningful.
- Use query parameters for optional filtering or additional values.
- Validate route values before using them in application logic.
Summary
Angular route parameters allow dynamic values to be included in application URLs.
A parameter such as :id is defined in the route, while ActivatedRoute can be used to read the value inside the component.
Route parameters are especially useful when opening specific course, topic or resource details. In the next topic, we will learn about Child Routes.