question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Sidenav does not set 'active' on current route, does not support routerLinkActive

See original GitHub issue

When you select a link in MaterializeCss’s sidenav, the link is set to active. The ng2-materialize sidenav doesn’t automatically set the class for a newly selected link and doesn’t appear to expose any way for the developer to do so. The standard way to do that in angular is to set the class for the item with routerLinkActive="['className']".

Proposal: replace <li [class.active]="active"> with <li [routerLinkActive]="['active']"> in sidenav-link.component.html and add the import for angular/router to the app module.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
IAMtheIAMcommented, Oct 19, 2017

It looks like we’ll be good to go, with the PR suggested at Materialize. https://github.com/Dogfalo/materialize/pull/5285 It will allow .active to apply to any element instead of li or a. Then this:

<mz-sidenav-link>
  <a routerLink="/dashboard" routerLinkActive="active">Dashboard</a>
</mz-sidenav-link>
<mz-sidenav-link>
  <a routerLink="/users" routerLinkActive="active">Users</a>
</mz-sidenav-link>

will work as expected.

1reaction
wnippercommented, Aug 28, 2017

In case anyone else experiences this issue, the workaround I used was to fill the list item with the anchor and set the class on the anchor. This doesn’t hit all use cases, but it does work as expected. Example:

<mz-sidenav-link>
  <a routerLink="dashboard" routerLinkActive="active">Dashboard</a>
</mz-sidenav-link>
<mz-sidenav-link>
  <a routerLink="users" routerLinkActive="active">Users</a>
</mz-sidenav-link>
Read more comments on GitHub >

github_iconTop Results From Across the Web

In Angular, how do you determine the active route?
I've been trying to set the active class without having to know exactly what's the current location (using the route name). The is...
Read more >
RouterLinkActive - Angular
Tracks whether the linked route of an element is currently active, and allows you to specify one or more CSS classes to add...
Read more >
Side Nav - Page Reload - Auto Expand with route parameters o
When reloading the page, if the current route has route parameters then the sub-menu mdb-accordion-item should auto expand. Actual behavior.
Read more >
Styling Active Router Link in Angular - GeeksforGeeks
Here we have provided the “routerLinkActive” which is routing functionality that automatically activate the current route, and we have to ...
Read more >
Add an active className to the link using React Router
2. useLocation Hook. React router has a hook which can be very useful to know the current route. The above method of using...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found