Как выполнить поиск "debounceTime"
Ваш компонент.html
<input #search (keyup)="searchTerm.next(search.value)">
<div *ngIf="results">
<!--slice is optional-->
<div *ngFor="let item of results |slice:0:10">
{{item.descripcion}}
</div>
</div>
В вашем компоненте .ts
//We defined three variables,
items:any[]; //our items
results:any[]; //Our items filtered
searchTerm = new Subject<string>(); //A subject
search(terms: Observable<string>) {
return terms.pipe(debounceTime(400),
distinctUntilChanged(),
switchMap(term => this.searchEntries(term)));
}
searchEntries(term) {
//here we can put others conditions to term
//like term.lenght>3, e.g.
if (!term)
return of(null); //Notice we return an observable -using Observable.of
term=term.toUpperCase();
return of( //Idem before. If we're ask about an observable
//Like httpClient.get, we not need "of"
this.items.filter(a=>a.searchProperty.indexOf(term)>=0)
);
}
ngOnInit() {
//we subscribe to get the items
this.http.get("assets/items.json").subscribe((res:any[])=>{
//we can do this.items=res ...
//this.items=res;
//...but better make a map and create a property "searchProperty"
//so, our filtered can be over this property. notice
//the map is executed only one time
this.items=res.map(x=>{
return {
...x,
searchProperty:x.descriptcion.toUpperCase()
+x.texto.toUpperCase()
+x.comment.toUpperCase()
}
});
//then, we subscribe to "this.search"
this.search(this.searchTerm)
.subscribe(results => {
this.results = results;
});
});
}
ПРИМЕЧАНИЕ. Код в значительной степени основан на https://alligator.io/angular/real-time-search-angular-rxjs/