亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 Angular 8 中創建過濾管道

在 Angular 8 中創建過濾管道

智慧大石 2023-12-19 20:37:11
我在 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()));


查看完整回答
反對 回復 2023-12-19
  • 1 回答
  • 0 關注
  • 139 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號