简体   繁体   English

Angular,Filter管道返回空数据/ false

[英]Angular, Filter pipe returns empty data/false

i'm trying to follow a custom filter pipe tutorial from this link , but when i run my project on the browser the table become empty. 我正在尝试通过此链接遵循自定义过滤器管道教程,但是当我在浏览器中运行项目时,表将变为空。 I'm not sure what i have to do to correct this. 我不确定该如何纠正。 I already use import import { FilterPipe } from './filter.pipe'; 我已经import { FilterPipe } from './filter.pipe';使用import import { FilterPipe } from './filter.pipe'; to app.component.ts and declaration in module.ts . 到app.component.ts并在module.ts中声明。

 //filter.pipe.ts transform(productList: any, term: any): any { if (term === undefined) return productList; //not sure, but i think it causes by this line return productList.filter(function(Product) { return Product.prdName.toLowerCase().includes(term.toLowerCase()); }) } 

 <form id="filter"> <input type="text" class="form-control" name="term" [(ngModel)]="term" placeholder="filter by name" /> </form> 

 <tr *ngFor="let product of productList | filter: term"> <td>{{product.prdName}}</td> <!--other table data--> <td> 

When i remove the | filter: term" 当我删除| filter: term" | filter: term" part the table data showed but when i put it back table data show nothing. Can somebody help me figure this out? Sorry for my bad English. | filter: term"部分,表数据显示出来,但是当我放回表数据时什么也没显示。有人可以帮我解决这个问题吗?对不起,我的英语不好。

in your component how is initializated the term variable? 在您的组件中如何初始化术语变量?

becouse it maybe is not undefined but it is null or empty (''), in the initial condition try comparing with null, undefined and ''. 因为它可能不是未定义的,但它为null或为空(''),在初始条件下,请尝试与null,undefined和''比较。

I guess you need to add a check if the term is null. 我猜您需要添加一项检查是否该术语为空。 You could implement your pipe as follows, 您可以按以下方式实现管道,

export class MyFilterPipe implements PipeTransform {

    transform(items: any[], term: any[]): any {
      if (!term) 
        return items;
      return items.filter(item => item.productName.indexOf(term) > -1);
    }
}

DEMO 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM