Steps to implement Agular Signals

Follow below steps to implement the debounce operator with effect():

Step 1: Create a new Angular application using the Angular CLI:

ng new my-app

Folder Structure


Dependencies:

"dependencies": {
"@angular/animations": "^17.3.0",
"@angular/common": "^17.3.0",
"@angular/compiler": "^17.3.0",
"@angular/core": "^17.3.0",
"@angular/forms": "^17.3.0",
"@angular/platform-browser": "^17.3.0",
"@angular/platform-browser-dynamic": "^17.3.0",
"@angular/platform-server": "^17.3.0",
"@angular/router": "^17.3.0",
"@angular/ssr": "^17.3.3",
"express": "^4.18.2",
"rxjs": "~7.8.0",
"tslib": "^2.3.0",
"zone.js": "~0.14.3"
},

Step 2: Go to the project folder

cd my-app

Step 3: Add the following codes in respective files

HTML
<!--app.component.html-->

<div>
    <h1>{{ title }}</h1>
    <input type="text" [formControl]="searchInput" [value]="searchInputSignal()">
</div>
JavaScript
//app.component.ts

import { JsonPipe, NgIf } from '@angular/common';
import { Component, effect, signal } from '@angular/core';
import { Router, RouterOutlet } from '@angular/router';
import { FormControl, FormsModule, ReactiveFormsModule } from '@angular/forms';
import { debounceTime, distinctUntilChanged } from 'rxjs';

@Component({
    selector: 'app-root',
    standalone: true,
    imports: [RouterOutlet, ReactiveFormsModule, FormsModule],
    templateUrl: './app.component.html',
    styleUrl: './app.component.css',
})
export class AppComponent {
    title: string = 'debounce-example';
    searchInput = new FormControl('');
    searchInputSignal = signal(this.searchInput.value);

    constructor() {
        effect(() => {
            this.searchInput.valueChanges
                .pipe(debounceTime(500), distinctUntilChanged())
                .subscribe((res) => {
                    this.searchInputSignal.set(res);
                    console.log('Performing search for:', res);
                });
        });
    }
}

In this example,

  1. We create a FormControl instance called searchInput.
  2. We create a new signal called searchInputSignal and initialize it with the initial value of the FormControl instance (this.searchInput.value).
  3. Inside the effect function, we subscribe to the valueChanges observable from the searchInput FormControl instance.
  4. We use the pipe function from RxJS to apply the debounceTime and distinctUntilChanged operators to the valueChanges observable.
  5. When a new value is emitted after the debounce time and being distinct from the previous value, the subscribe callback function is executed.
  6. Inside the subscribe callback, we update the searchInputSignal with the new value using this.searchInputSignal.set(res).
  7. We also log the new value to the console.


Output:



Angular Signals – debounce in effect()

In Angular, the effect() function creates side effects based on reactive signal values. The debounce operator is a powerful tool that can be used with effect() to control the rate at which the side effect is triggered. Debouncing is a technique that delays the execution of a function until a certain amount of time has passed since the last time it was invoked. This can be particularly useful in scenarios where you want to prevent excessive function calls, such as user input events or network requests.

Similar Reads

Prerequisites

Knowledge of Angular EffectsUnderstanding of RxJSPrior experience with NgRx...

What is Debounce operator?

The debounce operator is part of the rxjs package, which provides a set of utility functions for working with signals. When used in conjunction with effect(), it allows you to specify a time delay before the side effect is executed. If the signal value changes again within the specified time frame, the debounce period is reset, effectively delaying the side effect until there is a pause in the signal updates....

Features of debounce operator

The debounce operator offers several benefits:...

Steps to implement Agular Signals

Follow below steps to implement the debounce operator with effect():...

Contact Us