【发布时间】:2021-09-02 02:32:56
【问题描述】:
当用户在文本框上单击(在按键事件上)时,我试图触发 API 调用,我该怎么做 Angular。我在使用 debounce 方法时遇到错误 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>
【问题讨论】:
-
您在哪里以及如何初始化您的
this.searchField?看来你的属性没有初始化 -
@federicoscamuzzi 请注意你的舌头兄弟
-
@Lautre 我在构造函数
searchField: FormControl;之前初始化我的 this.searchField
标签: angular typescript rxjs