query parameter angular 8 code example

Example 1: angular router with wuery param

goProducts() {
  this.router.navigate(['/products'], { queryParams: { order: 'popular' } });
}

http://localhost:4200/products?order=popular

 constructor(private route: ActivatedRoute) { }

  ngOnInit() {
    this.route.queryParams
      .filter(params => params.order)
      .subscribe(params => {
        console.log(params); // { order: "popular" }

        this.order = params.order;
        console.log(this.order); // popular
      }
    );
  }

Example 2: angular http request query params

request(query) {
  let params = new HttpParams().set("keyword", query);
  return this.http.get(`Your_URL`, {params});
}

Example 3: angular set query params

constructor(private router: Router) { }

public myMethodChangingQueryParams() {
  const queryParams: Params = { myParam: 'myNewValue' };

  this.router.navigate(
    [], 
    {
      relativeTo: activatedRoute,
      queryParams: queryParams, 
      queryParamsHandling: 'merge', // remove to replace all query params by provided
    });
}