68

How can I add query parameters to routerLink?

@RouteConfig {
   {path: '/search',   name: 'Search', component: SearchCmp}
}  

Let' say I want to route to /search?q=asdf,

<a [routerLink]= " [ '/Search' , {q= 'asdf'}] ">Link 1</a>

this resolves to /search .

Is there a way to add query parameters without using:

this.router.navigate( ['Search', { q: 'asdf'}]);

or

<a href="/search?a=asdf"> Link 2 </a>

?

8
  • 1
    Yea, this is same question and answer as this: stackoverflow.com/questions/35188060/… Commented Feb 5, 2016 at 14:20
  • 4
    no it's not. i this question is about query parameters and not about route paramters Commented Feb 5, 2016 at 14:24
  • Please clarify. Do you want a generic way to add url params or are you looking for an Angular way? Commented Feb 5, 2016 at 14:25
  • im looking for an angular way. <a href="/search?a=asdf">Link></a> would trigger a page reload Commented Feb 5, 2016 at 14:28
  • Why do you need query parameters? You are handling the routes in Angular not queries. Your routes determine the queries so that means you need route parameters...? Commented Feb 5, 2016 at 14:50

2 Answers 2

111

If u need something as /search?q=asdf than you can simply use:

@RouteConfig {
   {path: '/search',   name: 'Search', component: SearchCmp}
}

//And to generate router Links use:

<a [routerLink]="['/Search']" [queryParams]="{q:'asdf'}"></a>

This will generate the href tag as <a href="/search" but on clicking the anchor tag will lead you to url /search?q=asdf. [queryParams] will let you add the query params with "?", otherwise they will be appended by ";". You can get this parameter in your SearchCmp using:

constructor(private _routeParams: RouteParams) {
   var queryParam = this._routeParams.get('q');
}
Sign up to request clarification or add additional context in comments.

3 Comments

oh yes, that's it. thanks man! any chance to generate the href tag as /search?q="asdf" ?
This does not answer the original question. This does: stackoverflow.com/a/39016381/1480995
As @MichaelOryl says, this not answer the question and produce curious URL like "search;q=asdf"
98

OP (Original Poster) asked how to add query parameters via router link, not a router parameters as @SaUrAbH MaUrYa answered.

To add query parameters you need to use [queryParams] binding:

<a [routerLink]="['/users']" [queryParams]="{ page: 1 }">next page</a>

3 Comments

This is exactly what I was looking for. The [queryParams] property binding is helpful when generating links dynamically, e.g. within a *ngFor loop: ``` <li *ngFor="let a of accounts"> <a [routerLink]="['/account']" [queryParams]="{ id: a.Id }">{{a.Name}}</a> </li> ``` I couldn't get this to work with the other construct as suggested by SaUrAbH MaUrYa (unless I needed a hardcoded query string value)
how to pass page number dynamically I try to use [queryParams]="{ page: {{myPageVariable}} }" and it is not working
[queryParams]="{ page: myPageVariable }"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.