10

Angular2 has a routerLinkActive directive which adds class to the element if the route is active. I want to add a class when route is inactive. Is there a way to do this?

cha-cha
  • 328
  • 3
  • 13
  • Can you define what you mean by "inactive" in this case? There can only be on active route at one point in time, and the rest would be "inactive"?.... or are you looking for a way to disable a route? – Per Feb 01 '17 at 14:37
  • 2
    You could do this with CSS. Set the routerlinkactive to `active` and then do `a:not(.active)`. – Seiyria Feb 01 '17 at 14:47
  • Yes, by "active" I mean current route. Unfortunately in my case other library uses the class and I can't override it using css. – cha-cha Feb 01 '17 at 16:26

1 Answers1

20
<a routerLink="/user/bob" routerLinkActive #rla="routerLinkActive" [ngClass]="rla.isActive ? 'classIfActive' : 'classIfNotActive'">
</a>
Scipion
  • 11,449
  • 19
  • 74
  • 139