Angular2 Is it possible to count the number of ngFor?

Hi, I am looping a json string using ngFor, but is it possible to show the number of loop elements and not show its value?

for example in my component i

items = {item1, item2, item3, item4}

and in the html template

<p *ngFor="let i of items>{{i}}</p>

then the result will be

item1
item2
item3
item4

but instead, since there are four elements in the object, I want to display the number 4 (the number of elements in the object).

 4

How can i achieve this?

Thank!

+4
source share
2 answers

This will display the number at each iteration:

<p *ngFor="let i of items>{{ items?.length }}</p>
+3
source

You do not need to loop the Json String, just use the length property.

TS Code:

 items: string[] = ['item1', 'item2', 'item3'];

Inside Html use it:

{{items.length}}
0
source

Source: https://habr.com/ru/post/1674529/


All Articles