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` and `router.isActive` do not work with arrays in queryParams.

See original GitHub issue

I’m submitting a…


[ ] Regression (a behavior that used to work and stopped working in a new release)
[x] Bug report  
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior

routerLinkActive and router.isActive do not work with arrays in queryParams.

Expected behavior

routerLinkActive and router.isActive should match correctly an URL which has arrays in queryParams.

Minimal reproduction of the problem with instructions

Here is a plunker that show what the issue is: http://plnkr.co/edit/CgMuRGMvsu2KAOm0aYOQ If you click on Link with an array in the plunker, the navigation is achieved, but the class active is not added, and the link is not red. Because there is a queryParams with an array.

However, if you click on Link with an integer, it works fine, and the link is highlighted in red.

Identification of the issue

I have looked for the issue in angular code, and I think the issue is caused by this method which compare queryParams: In file packages/router/src/url_tree.ts:

function containsQueryParams(
    container: {[k: string]: string}, containee: {[k: string]: string}): boolean {
  return Object.keys(containee).length <= Object.keys(container).length &&
      Object.keys(containee).every(key => containee[key] === container[key]);
}

The issue is that containee[key] === container[key] cannot work with arrays. [1] === [1] is false.

Maybe I can do a pull request to fix this.

What is the motivation / use case for changing the behavior?

I am using arrays in queryParams, to store some state in the URL, in order to be able to save the link and use it later. I could probably use a workaround ; sort and stringify my array before using it, but I prefer not to.

Environment


Angular version: 5.2.5


Browser:
- [x] Chrome (desktop) version XX
- [ ] Chrome (Android) version XX
- [ ] Chrome (iOS) version XX
- [x] Firefox version XX
- [ ] Safari (desktop) version XX
- [ ] Safari (iOS) version XX
- [ ] IE version XX
- [ ] Edge version XX

Issue Analytics

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

github_iconTop GitHub Comments

4reactions
Zaid-Al-Omaricommented, Mar 9, 2018

I have fixed the issue with the PR #22666. kindly review @jasonaden

3reactions
IliaVolkcommented, Jul 26, 2018

Any updates?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular - routerLinkActive and queryParams handling
The solution works with Angular 11. Sets the link as active regardless of different query params (only segments/path is compared).
Read more >
New Angular Router features in versions 7, 8, and 9 - Medium
make routerLinkActive work with query params which contain arrays. The routerLinkActive directive does not accept query params that contain ...
Read more >
Mastering RouterLink - Briebug Blog
The goal here is to give a comprehensive introduction to the topic of routing via the Angular template using the RouterLink directive.
Read more >
How to Mark a Link as Active in Angular, Even if the Query ...
We achieve this by putting the RouterLinkActive directive on the link in question; ... But what if there are query params in your...
Read more >
@angular/router@15.0.4 - jsDocs.io
ROUTES is a low level API for router configuration via dependency ... The resulting match information or null if the route should not...
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