我在 Web 開發方面有一些經驗,但對 Angular 還很陌生。我正在嘗試創建一個簡單的過濾器來根據文本輸入過濾表的一列。我遇到的問題是,當您在文本輸入中輸入單個字母時,所有結果都會被過濾掉。AnimalsComponent.tsimport { ApiService } from '../api.service';import { AnimalFilterPipe } from '../animal-filter.pipe'@Component({ selector: 'app-animals', templateUrl: './animals.component.html', styleUrls: ['./animals.component.css'], providers: [AnimalFilterPipe]})export class AnimalsComponent implements OnInit { animals = []; constructor(private apiService: ApiService) { } ngOnInit() { this.apiService.getA().subscribe((data: any[])=>{ console.log(data); this.animals = data; }) }}動物過濾管import { Pipe, PipeTransform } from '@angular/core';@Pipe({ name: 'animalFilter'})export class AnimalFilterPipe implements PipeTransform { transform(animals: any, term: string): any { //check if the search term is defined if(!animals || !term) return animals; //return updated animals array animals.filter(function(animal){ return animal.Animal.toLowerCase().includes(term.toLowerCase()); }) }}動物.html<div style="padding: 13px;"> <form id = "animalFilter"> <label>Filter by Animal:</label> <input type="text" [(ngModel)]= "term" [ngModelOptions]="{standalone: true}"/> </form> <table> <tr> <th>Hemisphere</th> <th>Type</th> <th>Animal</th> <th>Seasonality</th> <th>Location</th> <th>Time</th> <th>Price</th> </tr> <td align="center">TBD</td> </ng-template> </tr> </table></div>如果有人可以幫助我并給我一些關于我需要更改的內容以及如何更好地前進的建議,以便我可以創建更多的過濾管道和更多的自定義管道。
1 回答

互換的青春
TA貢獻1797條經驗 獲得超6個贊
您剛剛忘記了 return
中的語句transform
:
return animals.filter(animal => animal.Animal.toLowerCase().includes(term.toLowerCase()));
- 1 回答
- 0 關注
- 139 瀏覽
添加回答
舉報
0/150
提交
取消