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.

Keep Query Params for Specific Routes on Navigation

See original GitHub issue

🚀 feature request

Relevant Package

@angular/router

Description

I have an application with two pages:

  1. First, URL of which could contain query params, which represent current tab, sorting and pagination. E.g. /first?tab=cats&sortBy=age&sortOrder=asc&pageIndex=3&pageSize=25.
  2. Second, URL of which shouldn’t contain query params. E.g. /second.

When I navigate from First to Second, query parameters are removed as expected, since I don’t use queryParamsHandling.

Though, when I navigate back from Second to First, query parameters aren’t restored. This behavior is also expected, but I’d like to have a route configuration option to keep them.

The intention behind this in my case is that user should see the same filtered data set as he did before navigation, so he doesn’t need to set tab, sorting and pagination again manually.

I tried to achieve similar behavior by providing user a button, which simulates browser Back via window.history.back() and Angular built-in Location.back(). Unfortunately, both suffer from duplicated history records issue, so user has to click the button several times to actually go back, which is unacceptable from UX perspective.

I also found the following solutions, but neither of them work as needed.

Describe the solution you’d like

// app-routing.module.ts
{
  path: 'first',
  component: FirstComponent,
  keepQueryParams: true
},
{
  path: 'second',
  component: SecondComponent
}

Describe alternatives you’ve considered

The only alternative I see is manually subscribe to route change, keep query parameters in shared service and restore them when needed.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:5
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Halfistcommented, Jun 15, 2020

@Airblader Then probably keepQueryParams should be replaced with queryParamsStrategy: 'clean' | 'cache' | 'preserve' | 'merge' with 'clean' as default.

@atscott Yes, via router.navigate() or <a [routerLink]="first"> click.

0reactions
angular-automatic-lock-bot[bot]commented, Nov 9, 2020

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular2 router keep query string - Stack Overflow
There is a workaround using secondary routes as Angular will persist these across primary route navigation. First, add a ...
Read more >
A Neat Trick to Globally Preserve Query Params in Angular ...
It is based on simple axioms of web navigation. At the moment there is no way to preserve query params globally on all...
Read more >
A Neat Trick to Globally Preserve Query Params ... - Byte Limes
At the moment there is no way to preserve query params globally on all route navigations. For example, if you were checking for...
Read more >
QueryParamsHandling - Angular
How to handle query parameters in a router link. One of: "merge" : Merge new parameters with current parameters. "preserve" : Preserve current...
Read more >
How To Use Query Parameters with Angular Router
By default, the query parameters are lost on any subsequent navigation action. To prevent this, you can set queryParamsHandling to either ' ...
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