-
Notifications
You must be signed in to change notification settings - Fork 25.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
routerLinkActive not updating when routerLink changed #18469
Comments
I just ran into this as well. I have a tab setup where the tab links to a child like:
... and if I navigate from |
I think I see what is happening. I added a break-point in the algorithm that determines if the Perhaps there is a race condition with the |
I have a hack that seems to work. After looking at the source code, it looks like
Here, the It's janky, but it works as a stop-gap. |
I wrote my example up, in case it helps anyone else - https://www.bennadel.com/blog/3375-forcing-routerlinkactive-to-update-using-an-inputs-hack-in-angular-5-0-2.htm |
Thanks for sharing your temporary fix! |
Great fix @bennadel . Is this the only way anybody has found? Is there nothing like Also wanted to make sure the following similar scenario will be handled - and can confirm that your fix works works even if you have a dynamic I have several tabs with a shared router outlet for the 'current customer' - eg. 'Order / History / Notes'. Fortunately I was able to push
|
any progress? |
I have to write a dirty directive as a work around to force the update import {
AfterContentInit,
ContentChildren,
Directive,
Input,
OnChanges,
QueryList,
SimpleChanges,
} from '@angular/core';
import { RouterLinkActive } from '@angular/router';
@Directive({
selector: '[routerLink]',
})
export class RouterLinkDirective implements OnChanges, AfterContentInit {
@Input() routerLink: any[] | string;
@ContentChildren(RouterLinkActive, { descendants: true })
linkActives !: QueryList<RouterLinkActive>;
ngOnChanges(changes: SimpleChanges) {
if (changes.routerLink && this.linkActives && this.linkActives.first) {
this.linkActives.first.ngOnChanges(null);
}
}
ngAfterContentInit() {
this.linkActives.changes.subscribe(_ => this.linkActives.first.ngOnChanges(null));
}
} |
In waiting they fix angular/angular#18469
Can't believe that this issue is more than a year old. Such basic functionality and it's broken. |
Hi, I am a beginner in TS and Angular, so please be patient if I write complete nonsense, but while trying to understand what the problem is with routerLinkActive I found this in file router/src/directives/router_link_active.ts:
So if one of the lists is empty, I return. I must have both this.links and this.linksWithHrefs non-empty to not return, i.e. to go on and really update. Isn't this already a bug? |
I am using Angular 6.0.3 and this solve is working for me. Thank you @bennadel. |
Thank you so much @bennadel. I was facing this issue in Angular 5 also and did not know what to do. You saved my day. |
Since I was facing the same issue like #9825 - which I did not know I made a code sample on stackblitz As seen in the sample as long as |
Another hack for this would be to add markForCheck() in the component on router NavigationEnd
|
Hello Angular guys, @vsavkin, @petebacondarwin, @vicb, @IgorMinar, @tbosch, @gkalpak. Are you going to fix this once for all? Developers should not do hacks for such a basic functionality :D Best, Miroslav |
@scott-ho this.linkActives is allways undefined,, any idea?? <a |
@mackelito probably best to use a "take(1)" in the rxjs pipe here as well, since we really only to check this once, since after the first load the routerLinkActive directive works just fine- |
a simple solution is to subscribe to query.params and route params this.route.queryParams.subscribe(queryParams => {
the above work for me perfectly |
Hello - Any update on this issue? It's been over two years since the initial report and there is still no fix from what I can tell. I was using the I needed an immediate update since this was used for a side navigation component. Each side navigation item is a component as well. So to solve this I send the current route to the layout service as soon as an item is clicked. Then instead of calling It's not the most ideal, but it worked for my situation where the others did not. It's simple, can be easily modified to survive a browser refresh, etc. |
A false positive of this bug could be that when you pass in a router link of type In that case, make sure to first convert the |
Still having this issue in Angular 9 |
If I got the time I'm gonna try to dig into this issue, it's frustration that after almost 2 years this is still in the code. Now you can work around it with just some proper CSS. Meaning that your element has a normal state which has a class and the "active" state needs 2 classes to be present on the elment. Like this
|
Confirmed in v9 with repro: https://stackblitz.com/edit/angular-ivy-2wajtk?file=src%2Fapp%2Fapp.module.ts Duplicate of #13865, though this one is a more general case so I'll close 13865 and keep this open. |
You can use the following as well // MatTabNav reference
@ViewChild(MatTabNav) nav: MatTabNav;
this.subscriber = this.router.events.pipe(
filter((event: RouterEvent) => event instanceof NavigationEnd),
tap(() => this.nav.updateActiveLink())
).subscribe(); |
i use angular9 with ng-zorro, and the routerLink not work some time.
|
I managed to get it working in Angular 10 with ng-zorro without any of the hacks presented here. Instead of having links that will change value sometimes I created an observable emitting an array of objects with the information needed to generate my links, and then I iterate over that array in the html. Then it hightlights the links correctly after they change. I suspect that it works because I don't use a trackBy function, so the links are completely re-rendered every time the observable emits. <li *ngFor="let item of items$ | async" nz-menu-item nzMatchRouter>
<a [routerLink]="item.url">
<i nz-icon [nzType]="item.icon" nzTheme="outline"></i>
<span>
{{ item.label }}
</span>
</a>
</li> |
@DK8ALREC That's great you found another solution and I'm sure it will work great for those already using ng-zorro. However, for those not using ng-zorro it would be a massive dependency for something so trivial. It would be nice if the Angular team can just get an official patch for this already. On a side note, this issue hit it's 3rd birthday a couple of days ago! Happy 3rd birthday #18469. 🥳 🎈 🍰
|
…nks change This commit introduces a new subscription in the `routerLinkActive` directive which triggers an update when any of its associated routerLinks have changes. `RouterLinkActive` not only needs to know when links are added or removed, but it also needs to know about if a link it already knows about changes in some way. Quick note that `from...mergeAll` is used instead of just a simple `merge` (or `scheduled...mergeAll`) to avoid introducing new rxjs operators in order to keep bundle size down. Fixes angular#18469
…nks change This commit introduces a new subscription in the `routerLinkActive` directive which triggers an update when any of its associated routerLinks have changes. `RouterLinkActive` not only needs to know when links are added or removed, but it also needs to know about if a link it already knows about changes in some way. Quick note that `from...mergeAll` is used instead of just a simple `merge` (or `scheduled...mergeAll`) to avoid introducing new rxjs operators in order to keep bundle size down. Fixes angular#18469
…nks change This commit introduces a new subscription in the `routerLinkActive` directive which triggers an update when any of its associated routerLinks have changes. `RouterLinkActive` not only needs to know when links are added or removed, but it also needs to know about if a link it already knows about changes in some way. Quick note that `from...mergeAll` is used instead of just a simple `merge` (or `scheduled...mergeAll`) to avoid introducing new rxjs operators in order to keep bundle size down. Fixes angular#18469
…nks change This commit introduces a new subscription in the `routerLinkActive` directive which triggers an update when any of its associated routerLinks have changes. `RouterLinkActive` not only needs to know when links are added or removed, but it also needs to know about if a link it already knows about changes in some way. Quick note that `from...mergeAll` is used instead of just a simple `merge` (or `scheduled...mergeAll`) to avoid introducing new rxjs operators in order to keep bundle size down. Fixes angular#18469
…nks change This commit introduces a new subscription in the `routerLinkActive` directive which triggers an update when any of its associated routerLinks have changes. `RouterLinkActive` not only needs to know when links are added or removed, but it also needs to know about if a link it already knows about changes in some way. Quick note that `from...mergeAll` is used instead of just a simple `merge` (or `scheduled...mergeAll`) to avoid introducing new rxjs operators in order to keep bundle size down. Fixes angular#18469
…nks change This commit introduces a new subscription in the `routerLinkActive` directive which triggers an update when any of its associated routerLinks have changes. `RouterLinkActive` not only needs to know when links are added or removed, but it also needs to know about if a link it already knows about changes in some way. Quick note that `from...mergeAll` is used instead of just a simple `merge` (or `scheduled...mergeAll`) to avoid introducing new rxjs operators in order to keep bundle size down. Fixes angular#18469
…nks change This commit introduces a new subscription in the `routerLinkActive` directive which triggers an update when any of its associated routerLinks have changes. `RouterLinkActive` not only needs to know when links are added or removed, but it also needs to know about if a link it already knows about changes in some way. Quick note that `from...mergeAll` is used instead of just a simple `merge` (or `scheduled...mergeAll`) to avoid introducing new rxjs operators in order to keep bundle size down. Fixes angular#18469
…nks change (#38349) This commit introduces a new subscription in the `routerLinkActive` directive which triggers an update when any of its associated routerLinks have changes. `RouterLinkActive` not only needs to know when links are added or removed, but it also needs to know about if a link it already knows about changes in some way. Quick note that `from...mergeAll` is used instead of just a simple `merge` (or `scheduled...mergeAll`) to avoid introducing new rxjs operators in order to keep bundle size down. Fixes #18469 PR Close #38349
…nks change This commit introduces a new subscription in the `routerLinkActive` directive which triggers an update when any of its associated routerLinks have changes. `RouterLinkActive` not only needs to know when links are added or removed, but it also needs to know about if a link it already knows about changes in some way. Quick note that `from...mergeAll` is used instead of just a simple `merge` (or `scheduled...mergeAll`) to avoid introducing new rxjs operators in order to keep bundle size down. Fixes angular#18469
…nks change This commit introduces a new subscription in the `routerLinkActive` directive which triggers an update when any of its associated routerLinks have changes. `RouterLinkActive` not only needs to know when links are added or removed, but it also needs to know about if a link it already knows about changes in some way. Quick note that `from...mergeAll` is used instead of just a simple `merge` (or `scheduled...mergeAll`) to avoid introducing new rxjs operators in order to keep bundle size down. Fixes angular#18469
…nks change (#38511) This commit introduces a new subscription in the `routerLinkActive` directive which triggers an update when any of its associated routerLinks have changes. `RouterLinkActive` not only needs to know when links are added or removed, but it also needs to know about if a link it already knows about changes in some way. Quick note that `from...mergeAll` is used instead of just a simple `merge` (or `scheduled...mergeAll`) to avoid introducing new rxjs operators in order to keep bundle size down. Fixes #18469 PR Close #38511
…nks change (#38511) This commit introduces a new subscription in the `routerLinkActive` directive which triggers an update when any of its associated routerLinks have changes. `RouterLinkActive` not only needs to know when links are added or removed, but it also needs to know about if a link it already knows about changes in some way. Quick note that `from...mergeAll` is used instead of just a simple `merge` (or `scheduled...mergeAll`) to avoid introducing new rxjs operators in order to keep bundle size down. Fixes #18469 PR Close #38511
…nks change (angular#38349) This commit introduces a new subscription in the `routerLinkActive` directive which triggers an update when any of its associated routerLinks have changes. `RouterLinkActive` not only needs to know when links are added or removed, but it also needs to know about if a link it already knows about changes in some way. Quick note that `from...mergeAll` is used instead of just a simple `merge` (or `scheduled...mergeAll`) to avoid introducing new rxjs operators in order to keep bundle size down. Fixes angular#18469 PR Close angular#38349
…nks change (angular#38511) This commit introduces a new subscription in the `routerLinkActive` directive which triggers an update when any of its associated routerLinks have changes. `RouterLinkActive` not only needs to know when links are added or removed, but it also needs to know about if a link it already knows about changes in some way. Quick note that `from...mergeAll` is used instead of just a simple `merge` (or `scheduled...mergeAll`) to avoid introducing new rxjs operators in order to keep bundle size down. Fixes angular#18469 PR Close angular#38511
…nks change (angular#38349) This commit introduces a new subscription in the `routerLinkActive` directive which triggers an update when any of its associated routerLinks have changes. `RouterLinkActive` not only needs to know when links are added or removed, but it also needs to know about if a link it already knows about changes in some way. Quick note that `from...mergeAll` is used instead of just a simple `merge` (or `scheduled...mergeAll`) to avoid introducing new rxjs operators in order to keep bundle size down. Fixes angular#18469 PR Close angular#38349
…nks change (angular#38511) This commit introduces a new subscription in the `routerLinkActive` directive which triggers an update when any of its associated routerLinks have changes. `RouterLinkActive` not only needs to know when links are added or removed, but it also needs to know about if a link it already knows about changes in some way. Quick note that `from...mergeAll` is used instead of just a simple `merge` (or `scheduled...mergeAll`) to avoid introducing new rxjs operators in order to keep bundle size down. Fixes angular#18469 PR Close angular#38511
…nks change (angular#38349) This commit introduces a new subscription in the `routerLinkActive` directive which triggers an update when any of its associated routerLinks have changes. `RouterLinkActive` not only needs to know when links are added or removed, but it also needs to know about if a link it already knows about changes in some way. Quick note that `from...mergeAll` is used instead of just a simple `merge` (or `scheduled...mergeAll`) to avoid introducing new rxjs operators in order to keep bundle size down. Fixes angular#18469 PR Close angular#38349
…nks change (angular#38511) This commit introduces a new subscription in the `routerLinkActive` directive which triggers an update when any of its associated routerLinks have changes. `RouterLinkActive` not only needs to know when links are added or removed, but it also needs to know about if a link it already knows about changes in some way. Quick note that `from...mergeAll` is used instead of just a simple `merge` (or `scheduled...mergeAll`) to avoid introducing new rxjs operators in order to keep bundle size down. Fixes angular#18469 PR Close angular#38511
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
I have a routerLink URL that can change over time, but when the URL changes, routerLinkActive is not re-evaluated.
I'm submitting a...
Current behavior
Change a routerLink URL to match/not match the current URL. routerLinkActive class is not applied/removed.
Expected behavior
routerLinkActive is re-evaluated when its corresponding routerLink is updated.
Minimal reproduction of the problem with instructions
Click "link a" or "link b" to make them active, then click the switchLinks button to change the URLs. Nothing changes.
http://plnkr.co/edit/DHn4my9uAKJSIQSTIm1g?p=preview
What is the motivation / use case for changing the behavior?
I have multiple departments managed by the same UI, with department name as a variable in the URL. When the user switches departments, and the on screen links or URL is changed, the UI doesn't update the routerLinkActive.
I think it's because ContentChildren.changes doesn't fire if the contents of a directive is changed, only when directives are added or removed.:
https://github.com/angular/angular/blob/master/packages/router/src/directives/router_link_active.ts#L106
Maybe it's too bad for performance to fix this?
Environment
Angular version: 4.3
Mac
Browser:
The text was updated successfully, but these errors were encountered: