I am implementing a search functionality in my app I wanted to implement debounce time on key up. Can anyone please help me with this
HTML
<div class="mt-4">
<div class="form-group has-search">
<span class="fa fa-search form-control-feedback"></span>
<input type="search" class="form-control" [(ngModel)]="searchKeywords" (keyup)="getSmartSearchValues(searchKeywords)" (search)="clearSearch()" placeholder="Search here">
</div>
</div>
i have tries this, but it is showing error
searchTextChanged = new Subject<string>();
ngOnInit(): void {
this.subscription = this.searchTextChanged
.debounceTime(1000)
.distinctUntilChanged()
.mergeMap(search => this.getSmartSearchValues('', ''))
.subscribe(() => { });
this.getAllData();
if (this.CoffeeItemList.length === 0) {
this.empty = true;
}
this.getItemsCount('');
}
Errors
Property 'debounceTime' does not exist on type 'Subject<string>'.
Property 'subscription' does not exist on type 'AppComponent'
You can add a
debounceTimeto your FormControl.E.g.:
Or you can use
fromEventSee attached Stackblitz
Or you bind your
ngModelChangeto an Observable and usedebounceTimewith it:See Stackblitz2
Edit:
Additional you can find an example at Angular's official documentation for Practical observable usage (where
debounceTimeis used).