我是Angular的新手,我需要过滤来自API的值。我尝试过这种方式。但我得到ERROR TypeError: items.filter is not a function
,请您帮我解决这个问题。
HTML
<div class="container">
<div class="row mt-4 mb-3">
<div class="col-md-8">
<div class="input-group mb-3">
<input type="text" [(ngModel)]="searchText">
<div class="input-group-append">
<button id="btncat-search" class="btn btn-primary p-3" type="button">Search</button>
</div>
</div>
</div>
</div>
<div>
<div>
<div *ngFor="let cat of categories, let i=index" [ngClass]="((i % 2)==0) ? 'col-md-5 left-col':'col-md-4'">
<div>
<button type="button" class="btn">{{cat.name | filter : searchText}}</button>
<i class="fa fa-chevron-right"></i>
</div>
</div>
</div>
</div>
</div>
PIPE.TS
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'filter'
})
export class FilterPipe implements PipeTransform {
transform(items: any[], searchText: string): any[] {
if(!items) return [];
if(!searchText) return items;
searchText = searchText.toLowerCase();
return items.filter( it => {
return it.toLowerCase().includes(searchText);
});
}
}