388

NOTE: There are many different answers here, and most have been valid at one time or another. The fact is that what works has changed a number of times as the Angular team has changed its Router. The Router 3.0 version that will eventually be the router in Angular breaks many of these solutions, but offers a very simple solution of its own. As of RC.3, the preferred solution is to use [routerLinkActive] as shown in this answer.

In an Angular application (current in the 2.0.0-beta.0 release as I write this), how do you determine what the currently active route is?

I'm working on an app that uses Bootstrap 4 and I need a way to mark navigation links/buttons as active when their associated component is being shown in a <router-output> tag.

I realize that I could maintain the state myself when one of the buttons is clicked upon, but that wouldn't cover the case of having multiple paths into the same route (say a main navigation menu as well as a local menu in the main component).

Any suggestions or links would be appreciated. Thanks.

Lazar Ljubenović
  • 18,976
  • 10
  • 56
  • 91
Michael Oryl
  • 20,856
  • 14
  • 77
  • 117

31 Answers31

478

With the new Angular router, you can add a [routerLinkActive]="['your-class-name']" attribute to all your links:

<a [routerLink]="['/home']" [routerLinkActive]="['is-active']">Home</a>

Or the simplified non-array format if only one class is needed:

<a [routerLink]="['/home']" [routerLinkActive]="'is-active'">Home</a>

Or an even simpler format if only one class is needed:

<a [routerLink]="['/home']" routerLinkActive="is-active">Home</a>

See the poorly documented routerLinkActive directive for more info. (I mostly figured this out via trial-and-error.)

UPDATE: Better documentation for the routerLinkActive directive can now be found here. (Thanks to @Victor Hugo Arango A. in the comments below.)

random_user_name
  • 25,694
  • 7
  • 76
  • 115
jessepinho
  • 5,580
  • 1
  • 19
  • 19
  • Please note, that you need to add the above directive to all your links. Then `is-active` will be added as a class to the active link only. – Val Neekman Jun 25 '16 at 16:11
  • 7
    Is there any way for it to activate when the route's children are active? The code has an `@input` for options, but `exact: false` activates the class whenever the current route's siblings are also active. – Matej Jun 26 '16 at 10:45
  • @jessepinho Can you use this from router.navigate() ? If not, how can you use it with a router.navigate() ? – Gab Jun 30 '16 at 15:56
  • 1
    @GabrieleB-David I haven't tested it, but I would assume that `router.navigate()` would work just fine. `routerLinkActive` is simply an indicator of whether this link represents the active route. – jessepinho Jul 07 '16 at 10:27
  • 11
    In the given example, [routerLinkActive] is added to the tag, however, it can also be put on other elements if the class is required elsewhere.
    – Oblivion2000 Jul 20 '16 at 12:16
  • That's why I need a location name! ) – DenisKolodin Jul 22 '16 at 07:53
  • 3
    @jessepinho - Tried it - [routerLinkActive]="'active'" will only work if you also have [routerLink] in the a-tag. If you had (click)="navitageTo('/route')" instead of [routerLink], and in that function, you called this.router.navigate(route), the new component will load but your active CSS class won't be applied. I'm trying do something else in that function besides this.router.navigate(..). – Mickael Caruso Jul 24 '16 at 02:26
  • This also can work routerLinkActive="active", just don't forget to use routerLink="/some_link" , i.e
  • link
  • – user3728728 Dec 24 '16 at 10:41
  • @codeninja if the child route contains the parent route exactly then active class is applied to the parent. e.g going to 'articles/1' results in 'articles' link being active in my case. – S.. Jan 09 '17 at 09:51
  • 4
    The example in the official documentation could help you: https://angular.io/docs/ts/latest/api/router/index/RouterLinkActive-directive.html – Victor Hugo Arango A. Jan 15 '17 at 11:55
  • [routerLinkActiveOptions]="{exact: true}" – AzizStark Jul 28 '20 at 15:45