0

I have already add routing to my angular project. And it is working fine. But I have a issue with router active status.

I use router link to link the router and add router active to find the activated router.

I use bootstrap default nav bar in my project.And I add drop down to nav bar as follow.

<li class="nav-item dropdown" routerActive="active">
        <a class="nav-link dropdown-toggle" id="navbarDropdownMenuLink" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
          Dropdown link
        </a>
        <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
          <a class="dropdown-item" routerLink="/link-one">Link One</a>
          <a class="dropdown-item" routerLink="/link-two">Link Two</a>
          <a class="dropdown-item" routerLink="/link-three">Link Three</a>
        </div>
      </li>

But When user click router links router active is not working. This issue is only for dropdown but in single nav bar item works perfectly.

What is the issue with this?

1
  • you have used routerActive in wrong place also you should be using ngClass here since the active class is to be added to a different element than routerLink Commented Jun 20, 2019 at 6:45

3 Answers 3

2

If you want to add the active class to your li, you will have to use template references, or the router in your TS.

<li class="nav-item dropdown" [class.active]="link0rla.isActive || link1rla.isActive || link2rla.isActive">
  <a class="nav-link dropdown-toggle" id="navbarDropdownMenuLink" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
    Dropdown link
  </a>
  <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
    <a class="dropdown-item" routerLinkActive #link0rla="routerLinkActive" routerLink="/link-one">Link One</a>
    <a class="dropdown-item" routerLinkActive #link1rla="routerLinkActive" routerLink="/link-two">Link Two</a>
    <a class="dropdown-item" routerLinkActive #link2rla="routerLinkActive" routerLink="/link-three">Link Three</a>
  </div>
</li>

If you want to do it through the router :

get isLiActive() {
  return ['/link-one', '/link-two', '/link-three']
    .some(link => this.router.url.startWith(link));
}

constructor(private router: Router) {}
Sign up to request clarification or add additional context in comments.

Comments

0

You need to place routerActive in the same anchor tag <a></a> where routerLink is added.

It won't work if you place it in <li>

Try like this:

<a class="dropdown-item" routerLink="/link-one" routerActive="active" >Link One</a>

1 Comment

I try this but I want to add active css class to be is li element because when ever user click the link I want to highlight the main element it means li element. how to do that???
0

You must use ng-dropdown with angular :

<div ngbDropdown class="dropdown">
  <button class="btn btn-outline-primary" id="dropdownMenuLink" ngbDropdownToggle>Toggle dropdown</button>
  <div ngbDropdownMenu aria-labelledby="dropdownMenuLink">
    <button ngbDropdownItem ><a routerLink="link-one">Link One</a></button>
    <button ngbDropdownItem ><a routerLink="link-two">Link Two</a></button>
    <button ngbDropdownItem ><a routerLink="link-three">Link Three</a></button>
  </div>

I had the same problem recently

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.