Question
In an Angular application, how can you determine which route is currently active?
For example, when building navigation with Bootstrap, you may want to mark a link or button as active when its related component is displayed inside a router-outlet.
A simple click-based solution is not enough, because the same route might be reached from multiple places in the app, such as:
- a main navigation bar
- a local menu inside a feature component
So the goal is to detect the active route from Angular's router state itself, rather than manually tracking which button was clicked.
What is the recommended Angular approach for this?
Short Answer
By the end of this page, you will understand how Angular identifies the current route, how to highlight active navigation links with routerLinkActive, and when to use Router or ActivatedRoute for route-aware logic in components.
Concept
Angular keeps track of navigation through its Router. Whenever the URL changes, Angular matches that URL to a configured route and renders the corresponding component inside a router-outlet.
To determine the active route, Angular gives you a few tools:
routerLinkActivefor adding CSS classes to active links in templatesRouterfor checking the current URL in component codeActivatedRoutefor reading route-specific information such as params, query params, and child routes
For navigation styling, the simplest and most Angular-friendly solution is usually routerLinkActive.
Why this matters:
- You often need to highlight the current page in a navbar.
- You may want to show or hide UI based on the current route.
- You may need route-aware logic for breadcrumbs, tabs, guards, or layouts.
Instead of manually tracking clicks, Angular lets you derive the active route from the router state. This is more reliable because it works for:
- direct URL entry
- browser back/forward navigation
- programmatic navigation
- multiple links pointing to the same route
Mental Model
Think of Angular Router as a GPS for your app.
- The URL is the destination.
- The route configuration is the map.
- The router-outlet is the screen where the selected destination is shown.
routerLinkActiveis like a light on the dashboard that turns on when you are currently on that route.
If you tried to track active links only by button clicks, it would be like guessing where the car is based only on which button the driver pressed earlier. That breaks as soon as the user refreshes the page, uses the browser history, or lands directly on a URL.
The router state is the source of truth.
Syntax and Examples
For most navigation menus, use routerLink together with routerLinkActive.
<nav>
<a routerLink="/home" routerLinkActive="active">Home</a>
<a routerLink="/about" routerLinkActive="active">About</a>
<a routerLink="/contact" routerLinkActive="active">Contact</a>
</nav>
When the current URL matches /about, Angular adds the active CSS class to that link.
Exact matching
Sometimes a parent route should only be active for the exact path.
<a
routerLink="/home"
routerLinkActive=
[]=
>
Home
Step by Step Execution
Consider this template:
<a routerLink="/profile" routerLinkActive="active">Profile</a>
<router-outlet></router-outlet>
And suppose the user navigates to /profile.
What happens step by step
- The browser URL becomes
/profile. - Angular Router checks the route configuration.
- It finds the route mapped to
/profile. - Angular renders that route's component inside
router-outlet. - Angular evaluates directives attached to template elements.
routerLinkActivesees that the current route matches/profile.- Angular adds the
activeCSS class to the<a>element.
The rendered HTML will behave as if it were:
<a class=>Profile
Real World Use Cases
Determining the active route is common in real Angular apps.
Navigation bars
Highlight the current page in a top navbar or sidebar.
Tabs inside a feature page
A settings screen may have routes like:
/settings/profile/settings/security/settings/billing
Each tab can use routerLinkActive to show which section is open.
Breadcrumbs and page context
Components may inspect the current route to generate labels such as:
- Home / Products / Details
- Admin / Users / Edit
Conditional layouts
You might hide navigation or switch layout behavior on routes like:
/login/checkout/admin
Analytics and logging
Developers often listen to route changes to track page views or user flow.
Access control UI
You may disable or hide buttons depending on the current route and route data.
Real Codebase Usage
In real Angular projects, developers usually use different router tools for different jobs.
1. Use routerLinkActive for styling links
This is the standard template-level approach.
<a routerLink="/projects" routerLinkActive="active">Projects</a>
2. Use exact matching for root or parent links
<a
routerLink="/"
routerLinkActive="active"
[routerLinkActiveOptions]="{ exact: true }"
>
Home
</a>
3. Use Router.url for simple checks in component code
if (this.router.url.startsWith('/admin')) {
// show admin-specific UI
}
4. Use router events for reacting to navigation changes
Common Mistakes
1. Tracking active state manually with click handlers
This often breaks when users refresh the page, use the browser back button, or navigate programmatically.
Broken approach:
activeTab = 'home';
setActive(tab: string) {
this.activeTab = tab;
}
This tracks clicks, not the actual route.
Use the router state instead.
2. Forgetting exact matching
<a routerLink="/home" routerLinkActive="active">Home</a>
This may also be active for /home/details.
Fix:
<a
routerLink="/home"
routerLinkActive="active"
[routerLinkActiveOptions]="{ exact: true }"
>
Home
</a>
3. Using ActivatedRoute when is needed
Comparisons
| Approach | Best for | Template or Code | Pros | Cons |
|---|---|---|---|---|
routerLinkActive | Highlighting active navigation links | Template | Simple, built-in, automatic | Mostly for UI state |
Router.url | Checking current URL in logic | Code | Easy to read, useful in components | Manual string checks can be fragile |
ActivatedRoute | Params, query params, route data | Code | Great for route-specific data | Not the simplest choice for active-link styling |
| Manual click state | Temporary UI state only | Code | Simple to start with |
Cheat Sheet
<a routerLink="/path" routerLinkActive="active">Link</a>
<a
routerLink="/path"
routerLinkActive="active"
[routerLinkActiveOptions]="{ exact: true }"
>
Link
</a>
constructor(private router: Router) {}
isActive(): boolean {
return this.router.url === '/path';
}
this.router.events
.pipe(filter(event => event instanceof NavigationEnd))
.subscribe( {
.(..);
});
FAQ
How do I highlight the current route in Angular?
Use routerLinkActive on the navigation link and provide the CSS class to apply, such as active.
What is the recommended way to detect an active route in Angular templates?
The recommended template-based approach is routerLinkActive.
How do I match only the exact route in Angular?
Use [routerLinkActiveOptions]="{ exact: true }".
Should I use Router or ActivatedRoute to get the current route?
Use Router for the current URL and navigation events. Use ActivatedRoute for params, route data, and nested route information.
Why is manual click tracking a bad way to detect the active route?
Because it does not reflect real router state when users refresh, use browser history, or navigate directly by URL.
Can multiple links point to the same route and still become active?
Yes. If multiple links target the active route, Angular can mark them all as active using routerLinkActive.
Does routerLinkActive work with nested routes?
Yes, but parent links may also be marked active unless you use exact matching.
Mini Project
Description
Build a small Angular navigation bar that highlights the active page and shows the current URL in the component. This demonstrates both template-based active route styling and code-based route detection.
Goal
Create a navigation menu where the active link is styled automatically and the current URL is displayed after each navigation.
Requirements
- Create at least three routes such as Home, Products, and About.
- Add navigation links using
routerLink. - Highlight the active link using
routerLinkActive. - Use exact matching for the Home link.
- Display the current URL in a component using
Router.
Keep learning
Related questions
@Directive vs @Component in Angular: Differences, Use Cases, and When to Use Each
Learn the difference between @Directive and @Component in Angular, including use cases, examples, and when to choose each.
Angular (change) vs (ngModelChange): What’s the Difference?
Learn the difference between Angular (change) and (ngModelChange), when each fires, and which one to use in forms and inputs.
Angular @ViewChild Returning Undefined: Lifecycle, Child Components, and Fixes
Learn why Angular @ViewChild can be undefined, when it becomes available, and how to access child components correctly using lifecycle hooks.