Since ngModel is updating instantly how to put a delay.
Answer by Fredrik Lundin updated for Angular 6:
Template:
Component:
import ......
import { Subject, EMPTY } from 'rxjs';
import { debounceTime, distinctUntilChanged, switchMap } from 'rxjs/operators';
@Component{(
...
)}
export class YourComponent implements OnDestroy {
term$ = new Subject();
private searchSubscription: Subscription;
constructor() {
this.searchSubscription = this.term$.pipe(
debounceTime(1000),
distinctUntilChanged(),
switchMap(term => {
/*do something*/
return EMPTY;
})
).subscribe();
}
ngOnDestroy() {
//remember to unsubscribe on destroy
if (this.searchSubscription) {
this.searchSubscription.unsubscribe();
this.searchSubscription = null;
}
}
}