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.

RouterLinkActive not handling routerLink change

See original GitHub issue

I’m submitting a … (check one with “x”)

[x] bug report
[ ] feature request
[ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior I have a routerLink [‘/accounts’, currentAccount, ‘account-statement’], with currentAccount an attribute of the component. On initial load of route /accounts/1/account-statement, the routerLinkActive class is correctly set. But when I do : this.router.navigate([‘/accounts’, 2, ‘account-statement’]), the active class is not set, although currentAccount is correctly set to 2. currentAccount is set in ngOnInit like this (route is ActivatedRoute): this.route.params.map(params => params[‘id’]) .subscribe((id: number) => { console.log(id); this.currentAccount = id; });

Expected/desired behavior RouterLinkActive class should be properly set.

Reproduction of the problem Code available here : http://pastebin.com/RwCRpCXP

Please tell us about your environment:

  • Angular version: 2.0.0-rc.4 (router 3.0.0-beta.2)
  • Browser: [ all ]
  • Language: [ TypeScript 1.9 ]

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:14 (4 by maintainers)

github_iconTop GitHub Comments

6reactions
grungondolacommented, Dec 6, 2017

Quick update, I figured out a way to make this work for me, in case others are creating dynamic navigation links. You should wrap it in a quick timeout, like so:

this.setupLinks();

let ID = 1;

setTimeout(() => {
  this.router.navigate('PageName', ID);
}, 10);

Updated with values instead of variables, since that is likely why people are downvoting.

3reactions
grungondolacommented, Aug 14, 2017

I’m still hitting this issue in my project.

I do not run into this issue on initial load or when the user navigates with the navbar, but when I manually navigate in the code, it doesn’t update what is active. I change the links based on a drop-down and then navigate to the “Home” link of whatever the selection is, and it doesn’t mark that as active in the DOM. I think this may be because of how the lifecycle hits, trying to mark the active link before updating the links in the ngFor that I have in the navbar.

Read more comments on GitHub >

github_iconTop Results From Across the Web

routerLinkActive not updating when routerLink changed #18469
I have a routerLink URL that can change over time, but when the URL changes, routerLinkActive is not re-evaluated. I'm submitting a.
Read more >
Angular 6 routerLinkActive not working - Stack Overflow
I have tried importing router, routerlinkactive, and routerlink from @angular/core. I purposely left both different styles of using ...
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 >
routerlinkactive not working with queryparams
In our intro to the Angular router, we briefly saw how to navigate to a different route declaratively using the RouterLink directive. This...
Read more >
Angular Routerlinkactive - StackBlitz
<a routerLink='B'. routerLinkActive='active'>B</a>. <router-outlet></router-outlet>`,. styles: ['.active { border: solid; }'].
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