Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2: "router.navigate" Encoding error in the URL

When I submit a form, I want to navigate to another site and set the search values in the URL as parameters.

I've got following code:

searchOffers() {
    this.router.navigate([
      '/search?query= +
        this.search.value.FG
    ]);
  }

This Code work's, but the URL look like this after the routing.

localhost:4200/search%3Fquery%3D1 

(The query parameter value is 1).

With this URL I cant fetch the parameters.

How can I format the URL to this: localhost:4200/search?query=1 ?

like image 445
Marcel Avatar asked Apr 16 '17 16:04

Marcel


2 Answers

I've had the case where I get a string with the whole url including the query params.

In this case you can do this

    searchOffers() {
    this.router.navigateByUrl('/search?query= +
        this.search.value.FG);
    }
like image 92
Wissam Goghrod Avatar answered Nov 04 '22 22:11

Wissam Goghrod


Make use of the options parameter:

searchOffers() {
    this.router.navigate(['/search'], { queryParams: { query: this.search.value.FG } });
  }
like image 8
unitario Avatar answered Nov 04 '22 22:11

unitario