Angular / Angular Routing and Navigation

Navigating Between Angular Components

In this tutorial, you'll learn how to navigate between different components in your Angular application. We'll cover using routerLink, routerLinkActive and other router directives.

Tutorial 2 of 5 5 resources in this section

Section overview

5 resources

Explains configuring routes and enabling navigation in Angular applications.

Introduction

In this tutorial, we will learn how to navigate between different components in an Angular application. Angular provides powerful routing and navigation capabilities that can be leveraged to create complex applications with multiple views and components.

By the end of this tutorial, you will be able to:
- Understand routing in Angular
- Use the routerLink and routerLinkActive directives
- Navigate between different components

Prerequisites:
- Basic knowledge of Angular
- Familiarity with TypeScript
- Angular CLI installed on your machine

Step-by-Step Guide

Routing in Angular is a mechanism by which users can navigate through the application across different components. Angular provides the RouterModule to manage navigation.

The routerLink directive is used to link to routes. It's similar to the traditional 'href' in HTML, but works with Angular routes.

The routerLinkActive directive is used to add a CSS class to the element when the linked route is active. This is commonly used to highlight the active route in a navigation menu.

Code Examples

Let's create a simple Angular application with two components: HomeComponent and AboutComponent.

  1. Creating HomeComponent
// home.component.ts

import { Component } from '@angular/core';

@Component({
  selector: 'app-home',
  template: `
    <h1>Welcome to the Home Page!</h1>
  `
})
export class HomeComponent { }
  1. Creating AboutComponent
// about.component.ts

import { Component } from '@angular/core';

@Component({
  selector: 'app-about',
  template: `
    <h1>About Us</h1>
  `
})
export class AboutComponent { }
  1. Setting Up Routes

Set up routes for the two components in the app-routing.module.ts file.

// app-routing.module.ts

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { HomeComponent } from './home/home.component';
import { AboutComponent } from './about/about.component';

const routes: Routes = [
  { path: '', component: HomeComponent },
  { path: 'about', component: AboutComponent }
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }
  1. Using routerLink and routerLinkActive

Add navigation links in the AppComponent with routerLink and routerLinkActive directives.

<!-- app.component.html -->

<nav>
  <a routerLink="/" routerLinkActive="active">Home</a>
  <a routerLink="/about" routerLinkActive="active">About</a>
</nav>

<router-outlet></router-outlet>

In the code above, routerLink sets the navigation path, and routerLinkActive sets the CSS class to 'active' when the route is active.

Summary

In this tutorial, we covered the basics of navigation in Angular applications. We learned how to create routes and navigate between components using the routerLink and routerLinkActive directives.

For further learning, explore more advanced topics such as nested routes, route guards, and lazy loading.

Practice Exercises

  1. Create an application with three components and set up navigation between them.
  2. Enhance the navigation menu by highlighting the active route.
  3. Add a '404 Not Found' route and component that will be displayed when the user navigates to a non-existent route.

Remember, the more you practice, the more you'll understand and be comfortable with Angular routing and navigation!

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Word to PDF Converter

Easily convert Word documents to PDFs.

Use tool

Image Converter

Convert between different image formats.

Use tool

CSV to JSON Converter

Convert CSV files to JSON format and vice versa.

Use tool

Word Counter

Count words, characters, sentences, and paragraphs in real-time.

Use tool

MD5/SHA Hash Generator

Generate MD5, SHA-1, SHA-256, or SHA-512 hashes.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help