我在 Web 開(kāi)發(fā)方面有一些經(jīng)驗(yàn),但對(duì) Angular 還很陌生。我正在嘗試創(chuàng)建一個(gè)簡(jiǎn)單的過(guò)濾器來(lái)根據(jù)文本輸入過(guò)濾表的一列。我遇到的問(wèn)題是,當(dāng)您在文本輸入中輸入單個(gè)字母時(shí),所有結(jié)果都會(huì)被過(guò)濾掉。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; }) }}動(dòng)物過(guò)濾管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()); }) }}動(dòng)物.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>如果有人可以幫助我并給我一些關(guān)于我需要更改的內(nèi)容以及如何更好地前進(jìn)的建議,以便我可以創(chuàng)建更多的過(guò)濾管道和更多的自定義管道。
1 回答
互換的青春
TA貢獻(xiàn)1797條經(jīng)驗(yàn) 獲得超6個(gè)贊
您剛剛忘記了 return 中的語(yǔ)句transform:
return animals.filter(animal => animal.Animal.toLowerCase().includes(term.toLowerCase()));
- 1 回答
- 0 關(guān)注
- 181 瀏覽
添加回答
舉報(bào)
0/150
提交
取消
