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.

QueryParamsHandling 'merge' does not remove query parameters

See original GitHub issue

I’m submitting a…


[x] Bug report

Current behavior

When navigating to a route with query params and the 'merge' queryParamHandling strategy, if a specified query param is null the specified query param is kept if it previously existed.

Expected behavior

When navigating to a route with query params and the 'merge' queryParamHandling strategy, if a specified query param is null the specified query param is removed if it previously existed.

Minimal reproduction of the problem with instructions

  • an application with one route /
  • router.navigate(['/'], { queryParams: { hello: 'world' }, queryParamHandling: 'merge' }) => /?hello=world
  • router.navigate(['/'], { queryParams: { hello: null }, queryParamHandling: 'merge' } => /?hello=world (but expected /)

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

I have a route with multiple query params, including filters. The filters are updated independently of the other query params but I don’t want to wipe them out when filters are updated (that is why I use 'merge'). However, I currently cannot remove a query param with 'merge' currently and I expect to be able to because the provided value of null should override an existing value and remove the query param.

The current workaround I have is to use Object.assign and the snapshot of the previous query params with the default QueryParamHandling to get the behavior I desire.

Environment


Angular version: 4.2.3

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
bossqonecommented, Sep 12, 2017

I wanted to send PR, but I don’t know how to properly write test for Router (especially for createUrlTree). Solution provided by @skreborn should work, but I think we could remove empty props directly in Router.createUrlTree.

Here’s my proposal of Router.createUrlTree:

createUrlTree(commands: any[], navigationExtras: NavigationExtras = {}): UrlTree {
  const {relativeTo,          queryParams,         fragment,
         preserveQueryParams, queryParamsHandling, preserveFragment} = navigationExtras;
  if (isDevMode() && preserveQueryParams && <any>console && <any>console.warn) {
    console.warn('preserveQueryParams is deprecated, use queryParamsHandling instead.');
  }
  const a = relativeTo || this.routerState.root;
  const f = preserveFragment ? this.currentUrlTree.fragment : fragment;
  let q: Params|null = null;
  if (queryParamsHandling) {
    switch (queryParamsHandling) {
      case 'merge':
        q = {...this.currentUrlTree.queryParams, ...queryParams};
        break;
      case 'preserve':
        q = this.currentUrlTree.queryParams;
        break;
      default:
        q = queryParams || null;
    }
  } else {
    q = preserveQueryParams ? this.currentUrlTree.queryParams : queryParams || null;
  }

  // HERE IS INTRODUCED CHANGE
  if (q !== null) {
    q = this.removeEmptyProps(q);
  }
  return createUrlTree(a, this.currentUrlTree, commands, q !, f !);
}

And then Router.navigate should be changed to:

navigate(commands: any[], extras: NavigationExtras = {skipLocationChange: false}):
    Promise<boolean> {
  validateCommands(commands);
  return this.navigateByUrl(this.createUrlTree(commands, extras), extras);
}
0reactions
angular-automatic-lock-bot[bot]commented, Sep 12, 2019

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

Angular 5 remove query param - Stack Overflow
You can remove a query parameter by using the merge option of queryParamsHandling and passing in null for any params you wish to...
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 >
Angular URL State management with Query Params or Route ...
This blog post explains the process to maintain state using browser URL. In Angular there are couple of different ways to manage the...
Read more >
Query Parameters in Angular - TekTutorialsHub
queryParamsHandling :”. This is the default option. The angular removes the query parameter from the URL when navigating to the next route.
Read more >
55. Preserve or merge the query parameters by forwarding ...
Hi FriendsIn this video, we will see how to forward the query parameters with the URL by forwarding with queryparamsHandling in Angular.
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