I recently came across a tutorial on RxJS that demonstrated the use of debounce and distinctUntilChanged. I'm trying to implement it in Angular 6, but I'm facing some challenges.
Here is the code from the tutorial:
var observable = Rx.Observable.fromEvent(input,'input');
observable.map(event=>event.target.value)
.debounceTime(2000)
.subscribe({
next:function(value){
console.log(value)
}
}
This is my attempt at implementing it:
In the HTML file:
<input class="form-control" [(ngModel)]="userQuestion" type="text" name="userQuestion" id="userQuestions">
In the TypeScript file:
import { Subject,Observable } from "rxjs";
import { debounceTime,distinctUntilChanged } from "rxjs/operators";
ngOnInit() {
// initialization
this.userQuestion = new Observable;
this.userQuestion.pipe(
debounceTime(2000)).subscribe(val => {
console.log(val)
}
)
}
However, my implementation does not seem to work correctly. Can anyone provide guidance on how to make it work?