RouterLinkActive not handling routerLink change
See original GitHub issueI’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:
- Created 7 years ago
- Reactions:1
- Comments:14 (4 by maintainers)
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:
Updated with values instead of variables, since that is likely why people are downvoting.
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.