我正在使用ngFor循环8个json对象,我不仅要循环值,还要计算循环值的数量并显示数字 .
例如,如果json值为
Content:{
0:"Content1",
1:"Content2",
2:"Content3",
3:"Content4",
4:"Content5",
5:"Content6",
6:"Content7",
7:"Content8"
}
我不仅要显示'内容'的循环值,还要计算它们,以便结果如下所示 .
1 <- counting
Content1
2
Content2
3
Content3
4
Content4
5
Content5
6
Content6
7
Content7
8
Content8
2 回答
Demo
您可以只打印items数组的长度 .
迭代数组
关于文档:https://angular.io/guide/structural-directives#inside-ngfor和https://angular.io/api/common/NgForOf
假设你有一个iterable:
然后你可以迭代和计数:
迭代对象属性
如果要迭代对象而不是对象数组,请检查How to iterate object keys using *ngFor
要记录,您需要一个自定义管道:
那就是
更新
从Angular 6.1,您可以使用本机
KeyValuePipe
.https://blog.angular.io/angular-v6-1-now-available-typescript-2-9-scroll-positioning-and-more-9f1c03007bb6#ff4b
https://angular.io/api/common/KeyValuePipe
作为记录: