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

后端 未结 5 1867
盖世英雄少女心
盖世英雄少女心 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:44

    original

    AFAIK there is currently no way to do this directly. A hack would be to add a template variable to the content and use a ViewChildren(...) query to get the created items and count them.

    
      ...
    
    
    count: {{filteredItems?.length}}
    @ViewChildren('someVar') filteredItems;
    

    An alternative approach would be to pass a reference to a counter variable to the pipe like shown in https://plnkr.co/edit/Eqjyt4qdnXezyFvCcGAL?p=preview

    update (Angular >=4.0.0)

    Since Angular 4 *ngFor supports as

    
    

    which you can use in the template (inside the element that *ngFor is added to) like

      
    {{result?.length}}

提交回复
热议问题