73

Im using this:

    import { HttpParams } from '@angular/common/http';
    let params = new HttpParams();
    params.append('newOrdNum','123');

But this is not working, i dont append param in url. Any suggestion?

7
  • You want param like example.com/newordnum/123? Commented Sep 14, 2017 at 8:11
  • 2
    Possible duplicate of Why HttpParams doesn't work in multiple line in angular 4.3 Commented Sep 14, 2017 at 8:11
  • i want to append on existing params. I have something like this : example.com?param1=111 and now i want to append to get example.com?param1=111&newOrdNum=123 Commented Sep 14, 2017 at 8:14
  • @Jota.Toledo its not add param in url with your example Commented Sep 14, 2017 at 8:16
  • Are you trying to send a request with query parameters or to change the current state of the browser URL? If its the second, then you need to explain better what you are trying to archive Commented Sep 14, 2017 at 8:19

6 Answers 6

125

This could be achieved by using the Router class:

Using a component:

import { Router, ActivatedRoute } from '@angular/router';

@Component({})
export class FooComponent {
   constructor(
     private _route: ActivatedRoute,
     private _router: Router
   ){}

   navigateToFoo(){
     // changes the route without moving from the current view or
     // triggering a navigation event,
     this._router.navigate([], {
      relativeTo: this._route,
      queryParams: {
        newOrdNum: '123'
      },
      queryParamsHandling: 'merge',
      // preserve the existing query params in the route
      skipLocationChange: true
      // do not trigger navigation
    });
   }
}

For more info check this book and the angular Router API

Sign up to request clarification or add additional context in comments.

7 Comments

should I be able to see it in the URL or this is behind the scene?
when skipLocationChanges is true then the url does not show it. which is fine. but i havent figured out what happens when it is false... Do you know?
when your query params are changed, does that reflect in the url as well?
What if this was a google map and the latitude and longitude were to be appended to the url such that any panning action on the map by the user would also cause the query params in the url to get updated as the lat/lon would change?
When skipLocationChanges is false URL shows the params
|
43

I had to adjust Jota.Toledos answer a bit so that it works for me, I had to take out the second and the last property of the extras - object:

   navigateToFoo(){
     this._router.navigate([], {
      queryParams: {
        newOrdNum: '123'
      },
      queryParamsHandling: 'merge',
    });
   }

1 Comment

when your query params are changed, does that reflect in the url as well?
7

You should write

let params = new HttpParams();
params = params.append('newOrdNum','123');

1 Comment

And then (how to use the newly created params variable)? Otherwise this doesn't solve/do anything, and doesn't answer the question posted.
4
this.router.navigate(['.'], { relativeTo: this.route, queryParams: { 'a': 'b' }, queryParamsHandling: 'merge', skipLocationChange: true});

router: Router route: ActivatedRoute

Comments

2

You should use Router module. check this doc: https://angular.io/guide/router

You need import these modules: import { Router, ActivatedRoute, ParamMap } from '@angular/router';

3 Comments

how to only append on existing with router? without writing path
@None: are using Router
@None: read this doc please: angular.io/api/common/http/HttpParams
1

Make sure while passing query params value should not be undefined.

      this.router.navigate([yourRoute,
         queryParams : 
              {
                key : (yourValue ? yourValue : '')
              },
         queryParamsHandling: 'merge'
            ]);
      

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.