5

Is there a clean way to merge the current optional queryParams with an additional optional queryParam on a link in the template?

Current url: /search;brand=Trek

Desired goto link: /search;brand=Trek;start=1 (startCount will be incremented)

I know Angular supports the ability to merge normal queryParams (https://angular.io/docs/ts/latest/api/router/index/RouterLink-directive.html), but I don't think it works with optional queryParams.

That's why this doesn't work:

<a class="nav-btn"
   queryParamsHandling="merge"
   [routerLink]="[ { start: startCount+1 } ]">
    Next Page
</a>

Any suggestions?

2
  • Try adding { preserveQueryParams: true } Commented May 21, 2017 at 2:41
  • This is deprecated. Commented Jun 21, 2017 at 19:09

3 Answers 3

4

Try

<a class="nav-btn"
   queryParamsHandling="preserve" <--- instead of 'merge'
   [routerLink]="[ { start: startCount+1 } ]">
    Next Page
</a>
Sign up to request clarification or add additional context in comments.

1 Comment

Nope, didn't seem to work. It got rid of all of the previous params and added the new start param.
1

I don't think this is possible with the latest release of Angular, so here's my workaround solution:

Subscribe and save the params:

ngOnInit() {
  this.route.params.subscribe((params: Params) => {
    for(let param in params){
        if(params.hasOwnProperty(param)){
            console.log('param:', param);
            this.currentParams[param] = params[param];
        }
    }
  });
}

Then add additional optional params to currentParam object:

this.currentParams.start = this.startCount+1;

And re-navigate to your route with all the params:

this.router.navigate([ 'search', this.currentParams ]);

Comments

0

for preserving and merging on existing query params:

<a class="nav-btn" preserveQueryParams [routerLink]="['/yourRoute']" [queryParams]="{start: startCount+1}"></a> <a class="nav-btn" mergeQueryParams [routerLink]="['/yourRoute']" [queryParams]="{start: startCount+1}"></a>

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.