doorman
doorman

Reputation: 16959

routerLinkActive allow any query params

I am trying to use routerLinkActive with the following cases

'./path1' the link path1 should be active.

'./path1?param1=70000&param2=test2' the link path1 should be active.

'./path1/path2' the link path2 should be active.

 <a [routerLink]="./path1" [routerLinkActiveOptions]="{ exact: true }"
  [routerLinkActive]="['active']">My link</a>

with routerLinkActiveOptions path1 with queryparams is not active.

How can I configure routerLinkActive to work with any queryparameters?

Upvotes: 5

Views: 3384

Answers (2)

Meysam Sahragard
Meysam Sahragard

Reputation: 363

I also had the same problem and solved it as follows: (My Angular version is 13)

HTML:

<a
[routerLink]="'/path1'"
[queryParams]="{ sample: 'xxx' }"
[routerLinkActiveOptions]="routerLinkActiveOptions"
routerLinkActive="active" >My link</a>

ts:

routerLinkActiveOptions: IsActiveMatchOptions = {
matrixParams: 'ignored',
queryParams: 'ignored',
fragment: 'ignored',
paths: 'exact'
};

This code only actives links that have exact paths and ignores query params. You can read more about "IsActiveMatchOptions" at the following link: IsActiveMatchOptions

Upvotes: 4

Sasuke91
Sasuke91

Reputation: 94

You might want to see this question. He managed to make it works by adding another a tag but make it hidden, and wrap everything inside li tag. It looks like.

<li routerLinkActive="active">
    <a [routerLink]="['/view', 10]"                 
       [queryParams]="{'offset': 0, 'numberOfItems': 100}" 
       queryParamsHandling="merge">
        <div>View</div>
        <div><span class="badge" [innerHtml]="viewCount"></span></div>
    </a>
    <a [routerLink]="['/view', 10]" style="display:none;">                
        <div><span class="badge" [innerHtml]="viewCount"></span></div>
    </a>
</li>

Upvotes: 2

Related Questions