How can I get the total number of items using ngFor?
*ngFor="let aviso of avisosTest | async; let i = index"
I'm working with Firestore from Firebase with an Observable:
avisosTest: Observable<Aviso[]>;
I tried this but it did not work
{{ avisosTest.length }}
{{ avisosTest?.length }}
in Angular 4 you can do this:
*ngFor="let aviso of avisosTest | async as aviso; let i = index"
And then you can call {{ avisosTest.length }}
inside that tag.
Example
<ul>
<li *ngFor="let aviso of avisosTest | async as aviso; let i = index">
{{aviso}} - {{i + 1}} of {{aviso.length}}
</li>
</ul>
Working Demo - wait 2 seconds for data load
If you need it outside of the loop, you can separate async
and ngFor
, like:
<div *ngIf="avisosTest | async as aviso">
<ul>
<li *ngFor="let aviso of aviso; let i = index">
{{aviso}}
</li>
</ul>
<div>
{{aviso.length}} items total
</div>
</div>
Working Demo - wait 2 seconds for data load
I think you need
<div *ngIf="(avisosTest | async)?.length==0">No records found.</div>
or
<div *ngIf="(avisosTest | async)?.length>0">{{avisosTest.legnth}}</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With