Angular 4复选框管道过滤器

时间:2017-09-14 07:58:06

标签: javascript angular typescript

您好我正在尝试创建一个自定义管道过滤器,在选中复选框时过滤数据。我有一个工作地点列表,我想在检查某个工作地点时过滤。我提供的是与问题/问题相关的代码。

管道代码 -

@Pipe({
  name: 'checkcity'
})
export class CheckcityPipe implements PipeTransform {

  transform(check: any, checked: any): any {
    let [loc] = checked;
    console.log('checked',checked);
    return checked
            ? check.filter(city => {return city.location }) 
            : check;
  }

}

输入复选框 -

<input type="checkbox" value="" [(ngModel)]="checked" name="checked"> Bangalore

要过滤的数据 -

<div *ngFor="let joblist of jobList | checkcity: checked">
    {{joblist.location}}
</div>

1 个答案:

答案 0 :(得分:1)

您应将本地var重命名为joblist,将来源重命名为jobLists它们不应相同。你能试试下面的代码

吗?
@Pipe({
  name: 'checkcity'
})
export class CheckcityPipe implements PipeTransform {

  transform(check: any, checked: any): any {
    //let [loc] = checked;
    console.log('checked',checked);
    return checked
            ? check.filter(city =>  city.location == checked) 
            : check;
  }

}

<input type="checkbox" value="Bangalore" [(ngModel)]="checked" name="city"> Bangalore

<div *ngFor="let joblist of jobLists | checkcity: checked">
    {{joblist.location}}
</div>