如何使用 @Input 在 *ngFor 指令中设置管道

How to use @Input to set a Pipe in a *ngFor directive

我有一个组件有 3 个输入参数

  1. json_columns
  2. json_rows
  3. name_filter

如何才能将 name_filter 设置为过滤该组数据的管道?

component.ts

@Input('json_columns') columns:[{}];
@Input('json_rows') rows:[{}];

//Pipe 
@Input('name_filter') filter:string;

component.html

<tr *ngFor="let item of filas | filter: searchItem; let i=index" >
{{item.nombre}}
</tr>

一种方法是创建函数“getFilas()”并在函数中实现过滤器

看这个例子:https://stackblitz.com/edit/angular-filter-data-in-component

这是模板

<div *ngFor="let item of getData(); let i=index">
    {{i}} -  {{item.name}} {{ item | json }}
</div>

这是组件中的相关代码

  @Input() field: string;
  @Input() value: string;
  @Input() data: any[];

  getData() {
    return this.data.filter((item) => {
      return item[this.field] == this.value;
    })
  }