Sub topic 7
Working with Query Parameters and Fragments
This chapter assumes you already know how to define routes, use RouterLink, and read path parameters with ActivatedRoute or component inputs. Here we cover the optional parts of a URL: the query string and the fragment.
Where They Sit in the URL
A path parameter identifies a resource and is part of the route match. Query parameters and fragments are optional extras. They never affect which route matches.
| URL part | Example | Typical use |
|---|---|---|
| Path parameter | `/products/42` | Which item to show |
| Query parameters | `?sort=price&page=2` | Filtering, sorting, paging |
| Fragment | `#reviews` | Jumping to a section of the page |
Setting Them in Links and Code
RouterLink takes queryParams and fragment as separate inputs. Router.navigate accepts the same options in its extras object.
import { Component, inject } from '@angular/core';
import { Router, RouterLink } from '@angular/router';
@Component({
selector: 'app-product-toolbar',
standalone: true,
imports: [RouterLink],
template: `
<a routerLink="/products" [queryParams]="{ sort: 'price', page: 1 }">By price</a>
<a routerLink="/products/42" fragment="reviews">Reviews</a>
<button (click)="nextPage(2)">Page 2</button>
`
})
export class ProductToolbarComponent {
private router = inject(Router);
nextPage(page: number): void {
this.router.navigate([], {
queryParams: { page },
queryParamsHandling: 'merge'
});
}
}The empty commands array [] keeps the current path. queryParamsHandling decides what happens to the existing query string:
| Value | Behavior |
|---|---|
| default (`''`) | Replace all query params with the new ones |
| `'merge'` | Keep existing params and add or overwrite the new ones |
| `'preserve'` | Keep the current params and ignore the new ones |
Reading Them
With withComponentInputBinding() turned on, query parameters bind to matching @Input() properties, the same way path parameters do. Fragments do not bind to inputs, so you read them from ActivatedRoute.
import { Component, Input, inject } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { AsyncPipe } from '@angular/common';
@Component({
selector: 'app-product-list',
standalone: true,
imports: [AsyncPipe],
template: `
<p>Sort: {{ sort ?? 'none' }}, page {{ page }}</p>
<p>Fragment: {{ fragment$ | async }}</p>
`
})
export class ProductListComponent {
@Input() sort?: string;
@Input() set page(value: string | undefined) {
this._page = Number(value ?? 1);
}
get page(): number { return this._page; }
private _page = 1;
private route = inject(ActivatedRoute);
fragment$ = this.route.fragment;
}If you are not using input binding, subscribe to route.queryParamMap and call get('sort') or getAll('tag') on it.
Common Errors
- Reading only the snapshot.
route.snapshot.queryParamMapdoes not update when the user moves from?page=1to?page=2, because the router reuses the same component instance. Use the observable or input binding instead. - Losing filters on navigation. Without
queryParamsHandling: 'merge', changing one parameter wipes out all the others. - Fragments that don't scroll. The router only scrolls to the element with the matching
idif you enable anchor scrolling:provideRouter(routes, withInMemoryScrolling({ anchorScrolling: 'enabled' })). - Putting identity in the query string. If the page cannot render without a value, make it a path parameter.
Exercise
A user is on /products?sort=price&page=3. Your button calls router.navigate([], { queryParams: { page: 4 } }). What URL results, and how do you keep the sort order?
Show answer
The result is /products?page=4. The default handling replaces the whole query string, so sort is dropped. Add queryParamsHandling: 'merge' to get /products?sort=price&page=4.
Exercise
Your component reads this.route.snapshot.queryParamMap.get('page') in its constructor. Clicking "next page" changes the URL but the displayed page number stays the same. Why, and what is the fix?
Show answer
The router reuses the component when only the query string changes, so the constructor does not run again and the snapshot stays stale. Subscribe to route.queryParamMap, or bind page as an @Input with withComponentInputBinding().