0

How do we style an element with css-class "active" with the new angular2.rc3 & router3.0.0alpha6?

The old approaches do not seem to work any more and there is little documentation yet on the finer details of the new router...

TommyF
  • 6,660
  • 8
  • 37
  • 61
  • Welcome to StackOverflow. Please provide those "old approaches", it is always useful to point what you have tried before others will try to help you. Thanks. – YakovL Jun 23 '16 at 10:43
  • See if this helps - http://stackoverflow.com/questions/34597835/how-to-get-current-route – Sanket Jun 23 '16 at 11:12
  • 1
    or http://stackoverflow.com/questions/34323480/in-angular-2-how-do-you-determine-the-active-route – Günter Zöchbauer Jun 23 '16 at 11:29

2 Answers2

2

Thanks sanket to pointing me to the other thread!

It seems in v3 the router does have an easily accessible way to get the current route:

this.router.url

So a possible solution to style the active route component with a css class is:

<li [class.active]="isActiveRoute('/myroute')">
    <a [routerLink]="['/myroute']">
</li>

and in your navigation / menu component:

isActiveRoute(route: string) : boolean
{
    if(this.router.url === route)
    {
        return true;
    }
    else
    {
        return false;
    }
}
TommyF
  • 6,660
  • 8
  • 37
  • 61