Get length of array in ngFor after pipes transformation

前端 未结 4 2071
走了就别回头了
走了就别回头了 2021-02-19 10:57

I have the following template:

相关标签:
4条回答
  • 2021-02-19 11:29

    @Günter Zöchbauer {{(myArray | customPipe)?.length}} - Working as Expected

    0 讨论(0)
  • 2021-02-19 11:34

    Well, this is not well documented in the Angular doc, you can find under source code of Angular at -

    https://github.com/angular/angular/blob/master/packages/common/src/directives/ng_for_of.ts

    *ngFor accepts count params.

    constructor(public $implicit: T, public ngForOf: NgIterable<T>, public index: number,public count: number) {
    }
    

    So we can get the count like -

    <div *ngFor="let item of items | pipe; let l = count">
    <div>{{count}}</div>
    </div>
    
    0 讨论(0)
  • 2021-02-19 11:35
    <div *ngFor="let item of myArray | customPipe1 | customPipe2 as result">
      Here is the length of my ngFor : {{result.length}}
    </div>
    

    See also https://angular.io/api/common/NgForOf

    0 讨论(0)
  • 2021-02-19 11:39

    Another solution could be the following

    <div *ngFor="let item of myArray | customPipe1 | customPipe2; let l = count">
      Here is the length of my ngFor : {{l}}
    </div>
    

    Plunker Example

    See also

    • https://github.com/angular/angular/blob/master/packages/common/src/directives/ng_for_of.ts#L15-L17
    0 讨论(0)
提交回复
热议问题