Angular 2自定义管道文件管理器问题

时间:2017-07-03 10:22:22

标签: angular angular2-custom-pipes

我想根据男性和女性在employeemale.ts组件中使用自定义管道过滤数据当我在ngfor循环中使用自定义管道中的女性时,它会根据女性显示数据:

使用:

<ng-container *ngFor="let empLists of empList | filterdata:'female'">

但是当我使用男人时,它会显示所有的json数据

不能工作:

<ng-container *ngFor="let empLists of empList | filterdata:'male'">

这是我的组件html

<div class="widget box">
    <div class="widget-header"> <h4><i class="fa fa-bars"></i> Female Employe Details</h4></div>
</div>

当我根据女性过滤数据时:

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'filterdata'
})
export class FilterdataPipe implements PipeTransform {

  transform(empList: Array<any>, arg: string): any {
    debugger
    return empList.filter(function(empLists){
      //console.log(empLists)
           return empLists.gender.toLowerCase().includes(arg.toLowerCase());
    })
  }

}
<div class="widget-content">
<table class="table table-bordered">
   <thead>
    <tr>
      <th>#employee ID</th>
      <th>Name</th>
       <th>Gender</th>
    </tr>
  </thead>

       <ng-container *ngFor="let empLists of empList | filterdata:'male'">
 <tr>
      <th>{{empLists.id}}</th>
       <th>{{empLists.fullName}}</th>
       <th>{{empLists.gender}}</th>

    </tr>
  </ng-container>
  </tbody>
</table>

这是我的自定义管道filterdata.pipe.ts

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'filterdata'
})
export class FilterdataPipe implements PipeTransform {

  transform(empList: Array<any>, arg: string): any {
    debugger
    return empList.filter(function(empLists){
      //console.log(empLists)
           return empLists.gender.toLowerCase().includes(arg.toLowerCase());
    })
  }
}

1 个答案:

答案 0 :(得分:1)

只需使用相等而不是包含:

transform(empList: Array<any>, arg: string): any {
return empList.filter(function(emp){
  //console.log(empLists)
       return emp.gender.toLowerCase() === arg.toLowerCase();
});

使用include会失败,因为“女性”中有“男性”