22

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.

1

4 Answers 4

23

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

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

Comments

15

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.

1 Comment

Cheers! Been searching SO long for this. Docs seems wrong? But ofc - correct way hidden in the cheatsheet!
6

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

5 Comments

I am using it but it's giving me url like /Questions;queryParams=%5Bobject%20Object%5D.
For the root router optional parameters are represented in the querystring, for child routers they are added as matrix parameters (what you showed)
Yeah you are right, it's for child route. So, is there any solution to show query parameters for child routes rather than this matrix parameter?
Not as far as I know (except a custom route serializer) or maybe you can just add it to the root route.
Thanks for helping out.
2

You need to update your router.navigate

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.