Angular Navigation
Learn how to navigate between Angular routes using routerLink, Router, navigate() and navigateByUrl().
What is Navigation?
In Angular, navigation means moving from one route or application view to another without reloading the complete web page.
Angular provides routing features that allow users to open different views such as courses, dashboard, contact and enquiry pages.
Navigation can be performed directly from the template using routerLink or programmatically using the Angular Router service.
Angular Navigation Flow
Common Navigation Methods
routerLink
Used to navigate directly from an HTML template.
navigate()
Used for programmatic navigation using route segments.
navigateByUrl()
Navigates using a complete URL string.
Query Parameters
Additional values can be passed through the URL.
Using routerLink
routerLink is the simplest way to navigate between Angular routes from an HTML template.
Instead of manually changing the browser URL, Angular Router handles the navigation.
<a routerLink="/courses">
Courses
</a>
<a routerLink="/contact">
Contact
</a>
CIIT Training Institute Example
<nav>
<a routerLink="/home">
Home
</a>
<a routerLink="/courses">
Courses
</a>
<a routerLink="/enquiry">
Enquiry
</a>
<a routerLink="/contact">
Contact
</a>
</nav>
CIIT Training Institute
Home → Courses → Enquiry → Contact
Users can move between different application views without refreshing the complete page.
Programmatic Navigation
Sometimes navigation needs to happen after a button click, form submission or another operation.
In such cases Angular's Router service can be used to navigate programmatically.
import { Component } from '@angular/core';
import { Router } from '@angular/router';
@Component({
selector: 'app-course',
templateUrl: './course.component.html'
})
export class CourseComponent {
constructor(private router: Router) {
}
openCourses() {
this.router.navigate(['/courses']);
}
}
<button
type="button"
(click)="openCourses()">
View Courses
</button>
Using navigate()
The navigate() method accepts route segments in the form of an array.
It is useful when the destination route is built dynamically.
this.router.navigate(['/courses']);
this.router.navigate(['/contact']);
openCourse(courseId: number) {
this.router.navigate([
'/courses',
courseId
]);
}
Using navigateByUrl()
The navigateByUrl() method navigates using a complete URL string.
This can be useful when the complete destination URL is already known.
this.router.navigateByUrl('/courses');
this.router.navigateByUrl('/contact');
Navigation with Query Parameters
Query parameters allow additional information to be included in the URL.
For example, a course page can receive a category or filter value through the query string.
<a
[routerLink]="['/courses']"
[queryParams]="{
category: 'dotnet'
}">
View .NET Courses
</a>
this.router.navigate(
['/courses'],
{
queryParams: {
category: 'dotnet'
}
}
);
Relative Navigation
Relative navigation is used when navigation should happen from the current route instead of starting from the application root.
This is useful with nested or child routes.
this.router.navigate(
['details'],
{
relativeTo: this.route
}
);
routerLink vs navigate() vs navigateByUrl()
| Method | Used From | Purpose |
|---|---|---|
routerLink
|
HTML Template | Navigate when the user clicks a link or element. |
navigate()
|
Component / TypeScript | Programmatic navigation using route segments. |
navigateByUrl()
|
Component / TypeScript | Navigate using a complete URL. |
queryParams
|
Template / TypeScript | Pass additional values through the URL. |
Complete Navigation Example
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() {
this.router.navigate(['/courses']);
}
openContact() {
this.router.navigateByUrl('/contact');
}
}
<div class="dashboard">
<h2>
CIIT Training Institute Dashboard
</h2>
<a routerLink="/courses">
Courses
</a>
<button
type="button"
(click)="openCourses()">
Open Courses
</button>
<button
type="button"
(click)="openContact()">
Contact Institute
</button>
</div>
Important Points
Use routerLink when navigation is directly triggered from an HTML template.
Use the Angular Router service when navigation needs to happen from TypeScript code.
navigate() accepts route segments and is useful for dynamic routes.
Use meaningful routes and avoid unnecessary navigation logic inside components.
Navigation Best Practices
-
Use
routerLinkfor normal template-based navigation. -
Use
navigate()when navigation depends on application logic. -
Use
navigateByUrl()when the complete URL is already available. - Use query parameters only when additional URL information is required.
- Keep route paths meaningful and consistent.
- Avoid unnecessary full-page reloads in Angular applications.
Summary
Angular navigation allows users to move between different application views without reloading the complete page.
routerLink is commonly used inside templates, while the Angular Router service provides programmatic navigation through methods such as navigate() and navigateByUrl().
In the next topic, we will learn how to work with Route Parameters.