我有以下模板:獲取ngFor數組的長度後,管道改造
<div *ngFor="let item of myArray | customPipe1 | customPipe2; let l = length">
Here is the length of my ngFor : {{l}}
</div>
不幸的是長度不ngFor存在。我該如何解決這個問題,以便在我的ngFor中有長度可用?
我有以下模板:獲取ngFor數組的長度後,管道改造
<div *ngFor="let item of myArray | customPipe1 | customPipe2; let l = length">
Here is the length of my ngFor : {{l}}
</div>
不幸的是長度不ngFor存在。我該如何解決這個問題,以便在我的ngFor中有長度可用?
<div *ngFor="let item of myArray | customPipe1 | customPipe2 as result">
Here is the length of my ngFor : {{result.length}}
</div>
@Igor謝謝 - 修正 –
自從發佈最終的Angular 2版本以來,我期待這個工作,並且已經添加了很久的回覆:D [here](https://stackoverflow.com/a/39476552/2435473),+1鏈接 –
如何獲得ngFor以外的長度? – Luckylooke
看到另一個解決方案可以是以下
<div *ngFor="let item of myArray | customPipe1 | customPipe2; let l = count">
Here is the length of my ngFor : {{l}}
</div>
參見
好了,這不是很好的角度DOC文檔,你可以在下面的角源代碼中找到 -
https://github.com/angular/angular/blob/master/packages/common/src/directives/ng_for_of.ts
* ngFor接受計數PARAMS。
constructor(public $implicit: T, public ngForOf: NgIterable<T>, public index: number,public count: number) {
}
因此,我們可以得到這樣的計數 -
<div *ngFor="let item of items | pipe; let l = count">
<div>{{count}}</div>
</div>
您已經有了'myArray.length'我不明白的問題 – echonax
這取決於你想要什麼,你可以給更多的上下文?您可以在文檔中看到'ngFor'中可用的內容:https://angular.io/api/common/NgForOf – jonrsharpe
@echonax通過使用管道可以改變長度。例如一個過濾器函數:) – PierreDuc