Filtering an array in angular2

前端 未结 2 465
醉酒成梦
醉酒成梦 2020-11-30 06:31

I am looking into how to filter an array of data in Angular2.

I looked into using a custom pipe, but I feel this is not what I am looking for, as it seems more geare

2条回答
  •  北海茫月
    2020-11-30 07:23

    There is no way to do that using a default pipe. Here is the list of supported pipes by default: https://github.com/angular/angular/blob/master/modules/angular2/src/common/pipes/common_pipes.ts.

    That said you can easily add a pipe for such use case:

    import {Injectable, Pipe} from 'angular2/core';
    
    @Pipe({
        name: 'myfilter'
    })
    @Injectable()
    export class MyFilterPipe implements PipeTransform {
        transform(items: any[], args: any[]): any {
            return items.filter(item => item.id.indexOf(args[0]) !== -1);
        }
    }
    

    And use it:

    import { MyFilterPipe } from './filter-pipe';
    (...)
    
    @Component({
      selector: 'my-component',
      pipes: [ MyFilterPipe ],
      template: `
        
    • (...)
    ` })

    Hope it helps you, Thierry

提交回复
热议问题