Is it possible to change a single route parameter in the current route, while keeping all the other parameters? This is for use in a paging component, which will route to a new page while keeping the other parts of the current route the same.
Some examples:
- Default page to page 2:
orders?foo=foo&bar=bar
>orders?foo=foo&bar=bar&page=2
- Default page to page 2 (child):
orders/;foo=foo;bar=bar
>orders/;foo=foo;bar=bar;page=2
- Page 2 to 3 on child and parent with parameters:
orders/;foo=foo;page=2?bar=bar
>orders/;foo=foo;page=3?bar=bar
I have tried using a routerLink
, however this loses any additional parameters that are not originally part of the router link.
I have also tried using the current Router
to get an Instruction
for the current path, however changing the parameters of the Instruction
does not seem to have any effect when calling navigateByInstruction()
.
Why you simply don't do this:
It's quite easy. Let's say we have a method in our ListComponent that changes the page and keeps all other parameters intact (like search related parameters - can't change the page and forget we were searching for something:):
I used Underscore to assign a page parameter to the existing map of parameters from my private, injected RouteParams object. That map is updated on navigate so we're all good, just remember you can't change it directly, it's imutable.
Here is my implementation of a list of articles along with a pagination component used as a directive to provide pagination:
ArticleListComponent:
PaginationComponent:
Pagination Template (index.html) - I use bootstrap for styling
Usage in article list template:
The article service in my little project requests a range of articles from the server using the Range header.
Hope you find this helpful!
I would be nice if the activeRoute could return a simple clone of the current route [] to be manipulated before call to router.navigate(nextroutearray), but i did not find such method in the api.
I therefore centralized the requirement into a simple service, where i can parse the current activeRoute, + a set of parameters to be used for the next route. I do not think this code will cover all the complexity of the url in a route, but in my case, I only use parameters on the last part of the route, so it works for me:
The service method looks like this:
Then i can use this method in any component getting my sitemap service injected:
And in the html, the link to setNextTab method looks like this:
<li (click)="setNextTab('myTabName')">Next tab</li>
you can use query parameter option instead of parameter it is use like
in calling component
in the called component