276

I want to pass a query parameter prop=xxx.

This didn't work

<a [routerLink]="['/somepath', {queryParams: {prop: 'xxx'}}]">
  Somewhere
</a>
Nikita Fedyashev
  • 16,963
  • 11
  • 45
  • 76
Günter Zöchbauer
  • 558,509
  • 191
  • 1,911
  • 1,506
  • The syntax that you want to use is for matrix parameters and this is the form `Somewhere`, this gives you a matrix url parameters (semicolon ; instead of ? and & separators) and you can access this by ActivatedRoute.params instead activatedRoute.queryParams More information here http://stackoverflow.com/questions/35688084/how-get-query-params-from-url-in-angular2 and here http://stackoverflow.com/questions/2048121/url-matrix-parameters-vs-request-parameters – William Ardila Mar 23 '17 at 18:30
  • 2
    Query parameters and matrix parameters are the same. The only difference is when they are added to the root segment, they are serialized as query parameters, when they are added to a child segment, they are serialized as matrix parameters. – Günter Zöchbauer Mar 23 '17 at 18:32
  • Have some more differences check this http://web.archive.org/web/20130126100355/http://brettdargan.com/blog/2009/01/16/query-vs-matrix-params Also you can check the link parameter syntax in the angular doc here https://angular.io/docs/ts/latest/guide/router.html#!#link-parameters-array – William Ardila Mar 23 '17 at 18:43

4 Answers4

533

queryParams

queryParams is another input of routerLink where they can be passed like

<a [routerLink]="['../']" [queryParams]="{prop: 'xxx'}">Somewhere</a>

fragment

<a [routerLink]="['../']" [queryParams]="{prop: 'xxx'}" [fragment]="yyy">Somewhere</a>

routerLinkActiveOptions

To also get routes active class set on parent routes:

[routerLinkActiveOptions]="{ exact: false }"

To pass query parameters to this.router.navigate(...) use

let navigationExtras: NavigationExtras = {
  queryParams: { 'session_id': sessionId },
  fragment: 'anchor'
};

// Navigate to the login page with extras
this.router.navigate(['/login'], navigationExtras);

See also https://angular.io/guide/router#query-parameters-and-fragments

Günter Zöchbauer
  • 558,509
  • 191
  • 1,911
  • 1,506
37
<a [routerLink]="['../']" [queryParams]="{name: 'ferret'}" [fragment]="nose">Ferret Nose</a>
foo://example.com:8042/over/there?name=ferret#nose
\_/   \______________/\_________/ \_________/ \__/
 |           |            |            |        |
scheme    authority      path        query   fragment

For more info - https://angular.io/guide/router#query-parameters-and-fragments

Vivek Kumar
  • 1,919
  • 2
  • 18
  • 27
5

For those who want to pass dynamic querystring params, this worked for me:

assume you have component model

x = {name:'xyz'}

html:

<a [routerLink]="['../']" [queryParams]="{prop: x.name}">Somewhere</a>

this will generate link:

../?prop=xyz
BraveNewMath
  • 7,592
  • 5
  • 43
  • 49
3

You can check out this as well.

 <router-link
 
:to="{name:'router-name', params: { id:param1}, query:{link:query1}}"

>
 link name

 </router-link
>
Simas Joneliunas
  • 2,705
  • 15
  • 25
  • 32