use*_*134 2 ionic-framework angular
我试图让用户通过在文本框中搜索来搜索数据库以提取结果,并且我不希望每当在文本框中插入变量时都调用 API,而是在调用之前等待 3 秒到 API。我正在使用去抖动,但它不起作用
<ion-searchbar
[(ngModel)]="userSearch"
[debounce]="4000"
(ionInput)="liveSearch($event)"
placeholder="Search for Politics, Entertainment, Sports..."
></ion-searchbar>
Run Code Online (Sandbox Code Playgroud)
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);
});
}
}
Run Code Online (Sandbox Code Playgroud)
小智 6
您应该更改(ionInput)="liveSearch($event)"为(ionChange)="liveSearch($event)",因为 debounce 属性只会改变ionChange行为。