From what I understood, Here you only want to call your logic when the user is done typing and the input is not equal to the previous input.
This can be archived by using rxjs operators.
You first need to get the reference of your input element in .ts this can be archived by @ViewChild and then in ngAfterViewInit() using fromEvent() convert our event into observable, then by use of rxjs operators we can achieve the following:
pluck('target', 'value'), : pluck allows us to pluck value property from our event object.
debounceTime(1000),: Discard emitted values that take less than the specified time in parameter(milliseconds) you can change it as per your requirement.
distinctUntilChanged(): Only emit when the current value is different than the last.
.ts
import { Component, ElementRef, ViewChild } from '@angular/core';
import { fromEvent, Subscription } from 'rxjs';
import { debounceTime, distinctUntilChanged, pluck } from 'rxjs/operators';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
@ViewChild('searchInput') searchInput:ElementRef;
serchObs: Subscription;
ngAfterViewInit() {
this.serchObs = fromEvent(this.searchInput.nativeElement, 'input')
.pipe(
pluck('target', 'value'),
debounceTime(1000),
distinctUntilChanged()
)
.subscribe(value => {
// Your Logic
searchInputSubject.next(value);
});
}
ngDestroy() {
this.serchObs.unsubscribe();
}
}
.HTML
<input
matInput
type="text"
placeholder="Search"
#searchInput
/>
To find documentation click -> debounceTime, distinctUntilChanged,
pluck, RxJs Operators