Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set query parameters to url Angular2?

I have a requirement to set a url with query parameters like /Questions?id=1234&pageid=0. I have tried to do it via router.Navigate['/Questions?id=1234&pageid=0'] but no luck.

After navigation browser shows like /Questions%3Fid%3D1234%26pageid%3D0.

I have also tried it with setting routerLink="/Questions?id=1234&pageid=0", but same result.

Please suggest any solution to do it.I am using rc5 for angular2.

like image 315
amit_chauhan Avatar asked Aug 31 '16 09:08

amit_chauhan


4 Answers

You can pass them as item in the router, but outside the commands array:

[routerLink]="["/Questions"], {queryParams: {id:1234, pageid:0}}"

this generates what you want: /Questions?id=1234&pageid=0

Or you can use it programmaticaly:

this.router.navigate(['/Questions'], { queryParams: {id:1234, pageid:0} })

with router being an instance of @angular/router's Router

like image 58
Pstr Avatar answered Oct 23 '22 03:10

Pstr


I found one solution by giving router-link and query-parameters separate, like below:

<a [routerLink]="['/Questions']" [queryParams]="{id:1234,page:1}"></a>

This is working fine for me.

like image 26
amit_chauhan Avatar answered Oct 23 '22 02:10

amit_chauhan


You can pass them as items in the router commands array:

[routerLink]="['/Questions'] [queryParams]="{id:1234, pageid:0}} ]"

See also https://angular.io/docs/ts/latest/guide/router.html#!#query-parameters

like image 27
Günter Zöchbauer Avatar answered Oct 23 '22 03:10

Günter Zöchbauer


You need to update your router.navigate

this.router.navigate(['/Questions', {id: 1234, pageid: 0}]);
like image 36
Volodymyr Khmil Avatar answered Oct 23 '22 03:10

Volodymyr Khmil