Keep the search box in the URL
Instead of reading ActivatedRoute and calling the router by hand, linkedQueryParam gives you a signal that writes to the URL, and a URL that writes to the signal.
export class SearchPage {
private route = inject(ActivatedRoute);
private router = inject(Router);
search = toSignal(
this.route.queryParamMap.pipe(
map((params) => params.get('search')),
),
);
setSearch(search: string) {
this.router.navigate([], {
relativeTo: this.route,
queryParams: { search },
queryParamsHandling: 'merge',
});
}
} export class SearchPage {
search = linkedQueryParam('search');
}