typescript
46 lines · 6 steps
Building an active-route navbar in Angular
A standalone Angular navbar that highlights the current route and exposes it to assistive tech.
Explained by
highlit
1import { Component } from '@angular/core';
2import { RouterLink, RouterLinkActive } from '@angular/router';
3import { NgFor } from '@angular/common';
4
5@Component({
6 selector: 'app-navbar',
7 standalone: true,
8 imports: [RouterLink, RouterLinkActive, NgFor],
9 template: `
10 <nav class="navbar">
11 <a
12 routerLink="/"
13 routerLinkActive="active"
14 [routerLinkActiveOptions]="{ exact: true }"
15 >
16 Home
17 </a>
18
19 <a
20 *ngFor="let link of links"
21 [routerLink]="link.path"
22 routerLinkActive="active"
23 #rla="routerLinkActive"
24 [attr.aria-current]="rla.isActive ? 'page' : null"
25 >
26 {{ link.label }}
27 </a>
28 </nav>
29 `,
30 styles: [`
31 .navbar { display: flex; gap: 1rem; }
32 .navbar a { color: #556; text-decoration: none; padding: 0.5rem 0.75rem; }
33 .navbar a.active {
34 color: #1a73e8;
35 font-weight: 600;
36 border-bottom: 2px solid #1a73e8;
37 }
38 `],
39})
40export class NavbarComponent {
41 readonly links = [
42 { path: '/dashboard', label: 'Dashboard' },
43 { path: '/projects', label: 'Projects' },
44 { path: '/settings', label: 'Settings' },
45 ];
46}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1routerLinkActive toggles a CSS class automatically based on the current URL, so highlighting needs no manual state.
- 2Exporting a directive to a template reference variable lets you read its live state elsewhere in the template.
- 3Deriving aria-current from active state keeps the UI accessible without duplicating routing logic.
Related explainers
typescript
import { registerLocaleData } from '@angular/common'; import localeFr from '@angular/common/locales/fr'; import localeFrExtra from '@angular/common/locales/extra/fr'; import localeDe from '@angular/common/locales/de';
Locale-aware bootstrapping in Angular
i18n
localization
dependency-injection
Intermediate
8 steps
typescript
import { Module } from '@nestjs/common'; import { ConfigModule } from '@nestjs/config'; import * as Joi from 'joi';
Validating env config at boot in NestJS
configuration
schema-validation
environment-variables
Intermediate
8 steps
typescript
import { Inject, Injectable, Logger } from '@nestjs/common'; import { CACHE_MANAGER } from '@nestjs/cache-manager'; import { Cache } from 'cache-manager'; import { InjectRepository } from '@nestjs/typeorm';
A cache-aside country lookup in NestJS
cache-aside
dependency-injection
batch-lookup
Intermediate
8 steps
typescript
import { Injectable, effect, signal, computed } from '@angular/core'; interface Preferences { theme: 'light' | 'dark';
A signal-based preferences store in Angular
signals
state-management
persistence
Intermediate
7 steps
typescript
import { useEffect, useState } from "react"; interface Section { id: string;
Building a scroll-spy hook in React
custom-hooks
intersectionobserver
dom-observation
Intermediate
8 steps
typescript
import { Injectable, Scope, Inject, NotFoundException } from '@nestjs/common'; import { REQUEST } from '@nestjs/core'; import { Request } from 'express'; import { DataSource } from 'typeorm';
Per-tenant database connections in NestJS
multi-tenancy
connection-pooling
dependency-injection
Advanced
8 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/building-an-active-route-navbar-in-angular-explained-typescript-3fe8/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.