Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 5 remove query param

How would I remove a query parameter from the URL? For example from www.expample.com/home?id=123&pos=sd&sd=iii to www.expample.com/home?id=123&sd=iii

EDIT: This is my version:

this.activatedRoute.queryParams.subscribe(c => {
  const params = Object.assign({}, c);
  delete params.dapp;
  this.router.navigate([], { relativeTo: this.activatedRoute, queryParams: params });
}).unsubscribe();
like image 852
Nazar Kalytiuk Avatar asked Jan 31 '18 23:01

Nazar Kalytiuk


People also ask

Can http delete have query parameters?

There's nothing wrong with using DELETE on a collection and filtering by query parameters. Neither the REST dissertation nor the HTTP spec say anything about not doing this. This is different than the answer to the question that @Thilo linked to because the circumstances are different.

What is queryParamsHandling?

QueryParamsHandlinglinkHow to handle query parameters in a router link. One of: "merge" : Merge new parameters with current parameters. "preserve" : Preserve current parameters. "" : Replace current parameters with new parameters.


3 Answers

You can remove a query parameter by using the merge option of queryParamsHandling and passing in null for any params you wish to remove.

// Remove query params
this.router.navigate([], {
  queryParams: {
    'yourParamName': null,
    'youCanRemoveMultiple': null,
  },
  queryParamsHandling: 'merge'
})

This option is simpler and requires less work to ensure you are not removing other params. You also do not need to worry about cleaning up an observable subscription when your component is destroyed.

like image 116
epelc Avatar answered Oct 10 '22 07:10

epelc


UPDATE: @epelc's answer below is the up-to-date and correct way to do this: https://stackoverflow.com/a/52193044/5932590.


Unfortunately, there is no clear-cut way to do this currently: https://github.com/angular/angular/issues/18011. However, as jasonaden commented on the linked thread,

This could be done manually by merging the old and new query params, removing the key you don't want.

Here is one way to do that:

Let's say you have your queryParams stored in some properties.

class MyComponent() {
  id: string;
  pos: string;
  sd: string;

  constructor(private route: ActivatedRoute, private router: Router) {}

  ngOnInit() {
    this.route.url.subscribe(next => {
      const paramMap = next.queryParamMap;
      this.id = paramMap.get('id');
      this.pos = paramMap.get('pos');
      this.sd = paramMap.get('sd');
    });
  }
}

A method to clear the pos parameter would look like this:

clearPosParam() {
  this.router.navigate(
    ['.'], 
    { relativeTo: this.route, queryParams: { id: this.id, sd: this.sd } }
  );
}

This will effectively navigate to the current route and clear your pos query parameter, keeping your other query parameters the same.

like image 36
vince Avatar answered Oct 10 '22 07:10

vince


This is the best sulotion that i found, you can change url parameters

in constructor use

    private _ActivatedRoute: ActivatedRoute

then use this in init or in constructor body

    var snapshot = this._ActivatedRoute.snapshot;
    const params = { ...snapshot.queryParams };
    delete params.pos
    this.router.navigate([], { queryParams: params });
like image 3
Mohammad Hassani Avatar answered Oct 10 '22 09:10

Mohammad Hassani