typescript
57 lines · 8 steps
Debounced search that syncs to the URL in Angular
A standalone Angular component turns keystrokes into debounced signals and mirrors the search term in the query string.
Explained by
highlit
1import { Component, inject, effect } from '@angular/core';
2import { FormControl, ReactiveFormsModule } from '@angular/forms';
3import { ActivatedRoute, Router } from '@angular/router';
4import { toSignal } from '@angular/core/rxjs-interop';
5import { debounceTime, distinctUntilChanged, map, startWith } from 'rxjs';
6
7@Component({
8 selector: 'app-product-search',
9 standalone: true,
10 imports: [ReactiveFormsModule],
11 template: `
12 <input
13 type="search"
14 placeholder="Search products"
15 [formControl]="query"
16 />
17 @if (activeQuery()) {
18 <p>Showing results for “{{ activeQuery() }}”</p>
19 }
20 `,
21})
22export class ProductSearchComponent {
23 private readonly route = inject(ActivatedRoute);
24 private readonly router = inject(Router);
25
26 protected readonly query = new FormControl('', { nonNullable: true });
27
28 protected readonly activeQuery = toSignal(
29 this.route.queryParamMap.pipe(map((params) => params.get('q') ?? '')),
30 { initialValue: '' },
31 );
32
33 private readonly debouncedInput = toSignal(
34 this.query.valueChanges.pipe(
35 debounceTime(300),
36 map((value) => value.trim()),
37 distinctUntilChanged(),
38 startWith(''),
39 ),
40 { initialValue: '' },
41 );
42
43 constructor() {
44 this.query.setValue(this.activeQuery(), { emitEvent: false });
45
46 effect(() => {
47 const term = this.debouncedInput();
48 if (term === this.activeQuery()) return;
49
50 this.router.navigate([], {
51 relativeTo: this.route,
52 queryParams: { q: term || null, page: null },
53 queryParamsHandling: 'merge',
54 });
55 });
56 }
57}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Bridging RxJS streams to signals with toSignal lets template and effects read reactive state synchronously.
- 2Debouncing input before navigation avoids flooding the router with intermediate keystrokes.
- 3Guarding an effect against its own output prevents feedback loops when state flows both ways.
Related explainers
typescript
import { registerLocaleData } from '@angular/common'; import localeFr from '@angular/common/locales/fr'; import localeFrExtra from '@angular/common/locales/extra/fr'; import localeDe from '@angular/common/locales/de';
Locale-aware bootstrapping in Angular
i18n
localization
dependency-injection
Intermediate
8 steps
typescript
import { Module } from '@nestjs/common'; import { ConfigModule } from '@nestjs/config'; import * as Joi from 'joi';
Validating env config at boot in NestJS
configuration
schema-validation
environment-variables
Intermediate
8 steps
typescript
import { Inject, Injectable, Logger } from '@nestjs/common'; import { CACHE_MANAGER } from '@nestjs/cache-manager'; import { Cache } from 'cache-manager'; import { InjectRepository } from '@nestjs/typeorm';
A cache-aside country lookup in NestJS
cache-aside
dependency-injection
batch-lookup
Intermediate
8 steps
typescript
import { Injectable, effect, signal, computed } from '@angular/core'; interface Preferences { theme: 'light' | 'dark';
A signal-based preferences store in Angular
signals
state-management
persistence
Intermediate
7 steps
typescript
import { useEffect, useState } from "react"; interface Section { id: string;
Building a scroll-spy hook in React
custom-hooks
intersectionobserver
dom-observation
Intermediate
8 steps
typescript
import { Injectable, Scope, Inject, NotFoundException } from '@nestjs/common'; import { REQUEST } from '@nestjs/core'; import { Request } from 'express'; import { DataSource } from 'typeorm';
Per-tenant database connections in NestJS
multi-tenancy
connection-pooling
dependency-injection
Advanced
8 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/debounced-search-that-syncs-to-the-url-in-angular-explained-typescript-f2f0/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.