Angular 2 - ngFor index after a pipe

前端 未结 2 1238
旧巷少年郎
旧巷少年郎 2020-12-10 15:54

In Angular 2 when using ngFor how would I get the original index for an object within an array after it has been passed through a pipe?

For example if I have an arra

2条回答
  •  鱼传尺愫
    2020-12-10 16:35

    You can also use reduce method to keep original index:

    @Pipe({
      name: 'appFilter',
      pure: false
    })
    export class AppFilterPipe implements PipeTransform {
      transform(values: any[], arg1: any, arg2: any): any {
        return values.reduce((acc, value, index) => 
           value[arg1] === arg2 ? [...acc, { index, value }] : acc, []);
      }
    }
    

    and then in html

    {{item.index}} - {{item.value.id}}
    

    Plunker Example

提交回复
热议问题