79

How can I set Bootstrap navbar "active" class in Angular 2? I only found Angular 1 way.

When I go to About page, add class="active" to About, and remove class="active" on Home.

<ul class="nav navbar-nav">
    <li class="active"><a [routerLink]="['Home']">Home</a></li>
    <li><a [routerLink]="['About']">About</a></li></li>
</ul>

Thanks

Community
  • 1
  • 1
Hongbo Miao
  • 45,290
  • 60
  • 174
  • 267
  • 1
    Although it is not exactly what you are looking for, I believe that routerLink automatically sets the class to **router-link-active** when that link is active. So if its possible in your project, I would use the class .router-link-active in your css. – Gabu Feb 16 '16 at 02:29

11 Answers11

175

If you use the new 3.0.0. component router ( https://github.com/angular/vladivostok ) you can use the routerLinkActive directive. No further javascript required.

<ul class="nav navbar-nav">
  <li [routerLinkActive]="['active']"> <a [routerLink]="['one']">One</a></li>
  <li [routerLinkActive]="['active']"> <a [routerLink]="['second']">Second</a></li>
</ul>

I used "@angular/router": "^3.0.0-alpha.7"

Bert Deterd
  • 1,784
  • 1
  • 10
  • 6
71

Bert Deterd's answer is correct, but there's one thing that can be added.

If one route is a substring of another route, you will see something like this happen: 2 active anchors

You can add this to make it match exact routes only:

[routerLinkActiveOptions]="{exact:true}"

Full Example:

<ul class="nav navbar-nav">
  <li [routerLinkActive]="['active']" [routerLinkActiveOptions]="{exact:true}">
    <a [routerLink]="['/']">Home</a>
  </li>
  <li [routerLinkActive]="['active']" [routerLinkActiveOptions]="{exact:true}">
    <a [routerLink]="['/about']">About</a>
  </li>
  <li [routerLinkActive]="['active']" [routerLinkActiveOptions]="{exact:true}">
    <a [routerLink]="['/calendar']">Calendar</a>
  </li>
</ul>
Andrew Roth
  • 1,063
  • 10
  • 18
  • 1
    I ran into this with the home route being just the app domain (empty string). It always showed as active. – Vic Nov 06 '16 at 03:33
  • I ran into an issue, whereby clicking outside the link (for example in the center of the web) made the li unactive. This was because I had the angular directives in the anchor tag instead of the a tag. In my head this has no difference, but it works in your way, mybae this comment helps somebody else. Thanks! – Mese Feb 16 '18 at 15:36
  • The perfect answer – Sushin Pv Jul 15 '18 at 17:52
  • 3
    Your [routerLinkActiveOptions]="{exact:true}" parameter really helps thanks. For those that are interested in what it does it to stops the active class from activating on the ['/'] Home page when the user is on a different page because it matches the path exactly as it is. – Ali Kazai Feb 01 '20 at 02:59
  • amazing! Thanks! – PhillipJacobs Sep 04 '20 at 08:32
10

For the latest version of Angular2 RC4 the following simple code works:

      <li [class.active]="router.url==='/about'"><a [routerLink]="['/about']">About</a></li>

Using import {Router} from "@angular/router"; and put the router in the constructor.

Johannes
  • 2,732
  • 5
  • 23
  • 32
  • working fine for me (Y) . But i have a doubt that what i will do here incase of drop down menu.. example"
  • .". what we have to do if this is the scenario ? – Deepak Pookkote Jan 10 '18 at 11:54