How to get the size of a filtered (piped) set in angular2

后端 未结 5 1898
盖世英雄少女心
盖世英雄少女心 2020-12-15 17:20

I wrote my own filter pipe as it disappeared in angular2:

import {Pipe, PipeTransform} from \'angular2/core\';

@Pipe({
  name: \'myFilter\'
})
export class          


        
5条回答
  •  死守一世寂寞
    2020-12-15 17:22

    I don't know what you exactly want to do with the size and the Günter's solution can fit your needs.

    That said, you can inject the component instance into your pipe and set directly the length into a property of this component.

    @Pipe({
      name: 'dump'
    })
    export class DumpPipe {
      constructor(@Inject(forwardRef(() => AppComponent)) app:AppComponent) {
        this.app = app;
      }
    
      transform(array: Array, args: string): Array {
        (...)
        this.app.filteredItemLength = array.length;
    
        return array;
      }
    }
    
    @Component({
      (...)
    })
    export class AppComponent {
      (...)
    }
    

    See this answer:

    • How to save model manipulation of *ngFor with pipes? - "#item of (result = (items | orderAsc))" doesn't work in A2.

    Hope it helps you, Thierry

提交回复
热议问题