Angular Routing
Angular Routing allows an application to display different components based on the current URL. It helps build multi-page-like navigation experiences inside a single-page Angular application.
What is Angular Routing?
Angular Routing is the mechanism used to navigate between different views or components in an Angular application.
Instead of loading a completely new web page for every URL, Angular changes the displayed component while keeping the application running.
Simple Definition
Angular Routing connects URLs with Angular components and controls which component should be displayed for a particular route.
Angular Routing Flow
Main Angular Routing Concepts
Routes
Define the URL path and the component associated with it.
path: 'courses'
Router
Performs navigation between routes.
Router
RouterLink
Creates navigation links inside Angular templates.
routerLink
RouterOutlet
Displays the component associated with the current route.
router-outlet
1. Basic Route Configuration
Angular routes are defined using a route configuration array.
import { Routes } from '@angular/router';
export const routes: Routes = [
{
path: 'courses',
component: CoursesComponent
}
];
How it works
When the user navigates to the /courses URL, Angular can display the CoursesComponent.
2. Router Outlet
The router-outlet directive acts as a placeholder where Angular renders the component matched by the current route.
<nav>
<a routerLink="/courses">
Courses
</a>
</nav>
<router-outlet>
</router-outlet>
3. RouterLink Navigation
The routerLink directive is commonly used to navigate between routes from an Angular template.
<a routerLink="/courses">
Courses
</a>
<a routerLink="/contact">
Contact
</a>
4. Programmatic Navigation
The Angular Router can be used when navigation needs to happen from component code.
import { Component } from '@angular/core';
import { Router } from '@angular/router';
@Component({
selector: 'app-dashboard',
templateUrl: './dashboard.component.html'
})
export class DashboardComponent {
constructor(
private router: Router
) {
}
openCourses(): void {
this.router.navigate([
'/courses'
]);
}
}
<button
class="btn btn-primary"
(click)="openCourses()">
Open Courses
</button>
Use Case
Programmatic navigation is useful when navigation should happen after an operation such as login, form submission, or saving data.
5. Common Route Types
Root Route
Represents the default application route.
path: ''
Static Route
Uses a fixed URL path.
path: 'courses'
Parameterized Route
Includes a dynamic route parameter.
path: 'course/:id'
Child Route
Defines routes inside another route.
children
6. Default Route
An empty path can be configured as the default route for an application.
export const routes: Routes = [
{
path: '',
component: HomeComponent
}
];
Meaning
When the application is opened without another route path, Angular can display the HomeComponent.
7. Wildcard Route
A wildcard route can be used to handle URLs that do not match any configured route.
export const routes: Routes = [
{
path: 'courses',
component: CoursesComponent
},
{
path: '**',
component: NotFoundComponent
}
];
Unknown URL → Not Found Page
8. Route Matching
Angular evaluates the configured routes and selects the route that matches the current URL according to the router configuration.
export const routes: Routes = [
{
path: 'courses',
component: CoursesComponent
},
{
path: 'course/:id',
component: CourseDetailsComponent
},
{
path: '**',
component: NotFoundComponent
}
];
Important
Keep route configuration organized so that specific application routes are defined before a catch-all wildcard route.
9. Route Parameters Preview
Angular routes can contain dynamic parameters when a component needs to display information based on an identifier.
{
path: 'course/:id',
component: CourseDetailsComponent
}
/course/101
What is Next?
Route parameters are covered in detail in the Route Parameters topic.
10. Child Routes Preview
Child routes allow a feature area to contain routes nested under a parent route.
{
path: 'courses',
component: CoursesComponent,
children: [
{
path: 'details',
component: CourseDetailsComponent
}
]
}
11. Route Guards Preview
Route guards can control whether navigation to a route should be allowed based on application conditions.
{
path: 'admin',
component: AdminComponent,
canActivate: [
authGuard
]
}
Use Case
Route guards are commonly used for protected areas such as dashboards, admin pages, and authenticated features.
12. Navigation Methods
routerLink
Navigate directly from an Angular template.
routerLink="/courses"
navigate()
Navigate programmatically using route segments.
router.navigate()
navigateByUrl()
Navigate using a URL string.
navigateByUrl()
Back Navigation
Application logic can navigate back when required.
Location
Complete CIIT Training Institute Routing Example
Suppose CIIT Training Institute has separate pages for the home page, course list, and course details. Angular Routing can connect these views with clean URLs.
import { Routes } from '@angular/router';
export const routes: Routes = [
{
path: '',
component: HomeComponent
},
{
path: 'courses',
component: CoursesComponent
},
{
path: 'course/:id',
component: CourseDetailsComponent
},
{
path: '**',
component: NotFoundComponent
}
];
<nav>
<a routerLink="/">
Home
</a>
<a routerLink="/courses">
Courses
</a>
</nav>
<router-outlet>
</router-outlet>
import {
Component,
OnInit
} from '@angular/core';
import { ActivatedRoute }
from '@angular/router';
@Component({
selector: 'app-course-details',
templateUrl:
'./course-details.component.html'
})
export class CourseDetailsComponent
implements OnInit {
courseId = '';
constructor(
private route: ActivatedRoute
) {
}
ngOnInit(): void {
this.courseId =
this.route.snapshot
.paramMap
.get('id') ?? '';
}
}
Home: /
Courses: /courses
Course Details: /course/101
Example Course: Java Full Stack
Angular Routing Quick Reference
| Concept | Purpose | Example |
|---|---|---|
| Route | Maps URL path to component |
path: 'courses'
|
| routerLink | Template navigation |
routerLink="/courses"
|
| Router | Programmatic navigation |
router.navigate()
|
| RouterOutlet | Displays routed component |
<router-outlet>
|
| Route Parameter | Pass dynamic URL information |
:id
|
| Child Route | Create nested routes |
children
|
| Route Guard | Control route access |
canActivate
|
Important Points About Angular Routing
- Routes connect URL paths with Angular components.
- RouterLink is commonly used for template navigation.
- Router can be used for programmatic navigation.
- RouterOutlet displays the currently matched component.
- Route parameters allow dynamic information to be represented in a URL.
- Child routes and route guards support larger application navigation structures.
Angular Routing Best Practices
- Keep route paths meaningful and easy to understand.
- Use consistent route naming throughout the application.
- Use routerLink for normal template navigation.
- Use programmatic navigation when navigation depends on application logic.
- Organize child routes logically for larger features.
- Use route guards for protected application areas when required.
Summary
Angular Routing connects URLs with components and allows users to navigate between different views inside an Angular application.
Important routing features include Routes, Router, RouterLink, RouterOutlet, Route Parameters, Child Routes and Route Guards.