2017-06-13 45 views
5

我有以下模板:獲取ngFor數組的長度後,管道改造

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

不幸的是長度不ngFor存在。我該如何解決這個問題,以便在我的ngFor中有長度可用?

+0

您已經有了'myArray.length'我不明白的問題 – echonax

+0

這取決於你想要什麼,你可以給更多的上下文?您可以在文檔中看到'ngFor'中可用的內容:https://angular.io/api/common/NgForOf – jonrsharpe

+1

@echonax通過使用管道可以改變長度。例如一個過濾器函數:) – PierreDuc

回答

9
<div *ngFor="let item of myArray | customPipe1 | customPipe2 as result"> 
    Here is the length of my ngFor : {{result.length}} 
</div> 

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

+0

@Igor謝謝 - 修正 –

+1

自從發佈最終的Angular 2版本以來,我期待這個工作,並且已經添加了很久的回覆:D [here](https://stackoverflow.com/a/39476552/2435473),+1鏈接 –

+2

如何獲得ngFor以外的長度? – Luckylooke

8

看到另一個解決方案可以是以下

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

Plunker Example

參見

+0

不錯的看法:D https://github.com/angular/angular/blob/master/packages/common/src/directives/ng_for_of.ts#L21 – echonax

+1

@echonax是的,那是:) https://github.com /angular/angular/blob/master/packages/common/src/directives/ng_for_of.ts#L17 – yurzui