Narayan Prusty
Narayan Prusty

Reputation: 2581

Angular2 routerLink directive dynamic parameter?

Here is an example of routerLink

<a [routerLink]="['SearchResult', {myParam: 'value'}]">

Here myParam is constant. How can I make it dynamic i.e., use state to populate it.

Upvotes: 1

Views: 4710

Answers (1)

Ankit Singh
Ankit Singh

Reputation: 24945

Define an Object in your component and pass it to routerLink

Template

<a [routerLink]="['SearchResult', searchParams]">

Compoennt

@Component({.....})
export class YourComponent {
  public searchParams = {myParam: 'value'};

  ngAfterViewInit(){
     // change your parameters here. eg.: this.searchParams = {somePrm: 'someValue'}
  }
}

Alternatively if you only have minor changes or params to pass, you can also do this

<a [routerLink]="['SearchResult', (state == 'a') ?  {myParam: 'value'} :  {yourParam: 'value'}]">

Upvotes: 3

Related Questions