Sub topic 4
Navigating with RouterLink and Highlighting Active Links
This chapter assumes you know standalone components and template syntax, and that you have a Routes array registered with provideRouter and a <router-outlet /> in your root component. Here you add links that change the route without reloading the page.
Why Not a Plain `href`?
A normal <a href="/home"> makes the browser request a new document. That reload restarts your application and throws away its state. The RouterLink directive catches the click, cancels the browser's default behavior, and asks the Angular Router to navigate. The Router updates the URL with the History API and swaps the component inside the outlet.
Writing Links
Import RouterLink and RouterLinkActive into every standalone component that uses them.
import { Component } from '@angular/core';
import { RouterLink, RouterLinkActive, RouterOutlet } from '@angular/router';
@Component({
selector: 'app-root',
standalone: true,
imports: [RouterOutlet, RouterLink, RouterLinkActive],
template: `
<nav>
<a routerLink="/home" routerLinkActive="active">Home</a>
<a [routerLink]="['/products', 42]" routerLinkActive="active">Product 42</a>
</nav>
<router-outlet />
`,
styles: [`.active { font-weight: bold; text-decoration: underline; }`]
})
export class AppComponent {}routerLink takes either of two forms:
| Form | Example | Use when |
|---|---|---|
| String | `routerLink="/home"` | The path is fixed |
| Array of segments | `[routerLink]="['/products', product.id]"` | Parts of the path come from data |
When you use the array form, the Router builds each segment and URL-encodes it for you, so you don't have to concatenate strings. A path that starts with / is absolute. A path without the leading slash, such as routerLink="details", resolves relative to the current route.
Highlighting the Active Link
RouterLinkActive adds the classes you name while the link's URL is active, and removes them when it stops being active. You can list more than one class, for example routerLinkActive="active bold".
By default the match is partial. A link to /products stays active on /products/42. That works well for section menus, but it causes trouble for a root link:
<a routerLink="/" routerLinkActive="active"
[routerLinkActiveOptions]="{ exact: true }"
ariaCurrentWhenActive="page">Start</a>With exact: true, the link is active only when the URL matches it exactly. ariaCurrentWhenActive="page" sets aria-current="page" so screen readers announce the current link.
If you need the active state in the template, export the directive:
<a routerLink="/home" routerLinkActive #rla="routerLinkActive">
Home {{ rla.isActive ? '(current)' : '' }}
</a>Common Errors
Can't bind to 'routerLink': you used[routerLink]without importingRouterLinkinto the component. The plain attribute form fails silently instead. The link renders but does nothing Angular-specific.- Root link always highlighted: a link to
/partially matches every URL. Add{ exact: true }. - Wrong relative target:
routerLink="edit"inside/products/42points to/products/42/edit. Use../editor an absolute path if you meant something else. - Using
hreftogether withrouterLink: leave outhref. RouterLink sets it for you on anchor elements.
Exercise
Your nav has links to "/" and "/products". On the URL /products/7, both links get the "active" class. Fix the markup so only the Products link is highlighted, and screen readers are told which link is current.
Show answer
Add exact matching to the root link and set ariaCurrentWhenActive on both: <a routerLink="/" routerLinkActive="active" [routerLinkActiveOptions]="{ exact: true }" ariaCurrentWhenActive="page">Start</a> and <a routerLink="/products" routerLinkActive="active" ariaCurrentWhenActive="page">Products</a>. The Products link keeps partial matching, so it stays active on /products/7.
Exercise
Write a RouterLink that points to /products/{{id}}/reviews, where id is a component property, without string concatenation.
Show answer
<a [routerLink]="['/products', id, 'reviews']">Reviews</a>