How can I trigger an API call in Angular when a user clicks on a textbox during a keypress event? I am encountering an error with the debounce method that says
Cannot read property 'valueChanges' of undefined
app.component.ts
ngOnInit() {
this.searchField.valueChanges
.pipe(
debounceTime(500),
distinctUntilChanged(),
map((val) => {
this.http.post<any>('http://localhost:3000/articles/articleslistData', { pubid: '3', pubdate: this.dateChanged }).subscribe({
next: data => {
console.log(data);
},
error: error => {
this.errorMessage = error.message;
console.error('There was an error!', error);
}
})
})
)
.subscribe();
}
app.component.html
<div class="form-field col-lg-12">
<label class="label" for="message">Headline</label>
<input id="message" class="input-text js-input" type="text" required [formControl]="searchField">
</div>