我试图让用户通过在文本框中搜索来搜索数据库以提取结果,并且我不希望每当在文本框中插入变量时调用 API,但在调用 API 之前等待 3 秒。我正在使用去抖动,但它不起作用
<ion-searchbar
[(ngModel)]="userSearch"
[debounce]="4000"
(ionInput)="liveSearch($event)"
placeholder="Search for Politics, Entertainment, Sports..."
></ion-searchbar>
.JS
import { Component } from '@angular/core';
import { NewsService } from '../services/news-service.service';
@Component({
selector: 'app-tab3',
templateUrl: 'tab3.page.html',
styleUrls: ['tab3.page.scss']
})
export class Tab3Page {
userSearch: any;
searchResults: any= [];
constructor(private newsService: NewsService) {}
liveSearch() {
this.newsService.searchNews(this.userSearch).subscribe(data => {
this.searchResults = data;
console.log(data);
});
}
}
你应该把(ionInput)="liveSearch($event)"
改为(ionChange)="liveSearch($event)"
,因为去抖属性只会改变ionChange
行为。