Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set element as active when either of the sublink routes are active angular2

I have the following code snippet for generating bootstrap nav-taps with an embedded dropdown list:

    <ul class="nav nav-tabs">
      <li class="pull-right">
        <a class="dropdown-toggle" data-toggle="dropdown"> More <span class="caret"></span></a>
          <ul class="dropdown-menu">
            <li><a [routerLink]="['/profile/settings']">Settings</a></li>
            <li><a [routerLink]="['/profile/billing']">Billing</a></li>
          </ul>
      </li>
      <li class="pull-right" [routerLinkActive]="['active']"><a [routerLink]="['/profile/feature1']">Feature1</a></li>
      <li class="pull-right" [routerLinkActive]="['active']"><a [routerLink]="['/profile/feature2']">Feature2</a></li>
    </ul>

This works fine for the normal tabs, but I can't figure out how to make the dropdown list active when either of the embedded links are active. I tried:

<a [routerLinkActive]="['active']" class="dropdown-toggle" data-toggle="dropdown"> More <span class="caret"></span></a>

but it doesn't seem to solve anything, anyone know how I can do this?

like image 712
Syntactic Fructose Avatar asked Dec 19 '16 04:12

Syntactic Fructose


People also ask

How do I know if my routerLink is active?

routerLinkActive is simply an indicator of whether this link represents the active route. @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.

How does angular determine active routes?

Approach: To determine which is the active route at any moment in AngularJS this can be achieved by using $on() & $location. path() method.

Which of the following is used to make anchor elements visually distinguished for active route?

The RouterLinkActive directive can also be used to set the aria-current attribute to provide an alternative distinction for active elements to visually impaired users.


1 Answers

I'm dumb and attempted putting the [routerLinkActive] on the <a> element instead of <li>. Once I put it on the top most li it worked like a charm!

<ul class="nav nav-tabs">
  <li class="pull-right" [routerLinkActive]="['active']">
    <a class="dropdown-toggle" data-toggle="dropdown"> More <span class="caret"></span></a>
      <ul class="dropdown-menu">
        <li><a [routerLink]="['/profile/settings']">Settings</a></li>
        <li><a [routerLink]="['/profile/billing']">Billing</a></li>
      </ul>
  </li>
  <li class="pull-right" [routerLinkActive]="['active']"><a [routerLink]="['/profile/feature1']">Feature1</a></li>
  <li class="pull-right" [routerLinkActive]="['active']"><a [routerLink]="['/profile/feature2']">Feature2</a></li>
</ul>
like image 118
Syntactic Fructose Avatar answered Sep 22 '22 23:09

Syntactic Fructose