如何在Angular 2中迭代JSON对象?

时间:2021-01-14 13:38:41

Let's say I have a JSON like this:

假设我有一个像这样的JSON:

{
    "A": [
            {"name": "Aa", "count": "21"},
            {"name": "Ab", "count": "54"}
         ],

    "B": [
            {"name": "Ba", "count": "30"},
            {"name": "Bb", "count": "22"}
         ],

    "C": [
            {"name": "Ca", "count": "40"},
            {"name": "Cb", "count": "33"}
         ],
}

Output HTML should like this:

输出HTML应该是这样的:

[A]
Aa 21
Ab 54

[B]
Ba 30
Bb 22

[C]
Ca 40
Cb 33

How can I iterate using angular2 Beta version?

如何使用angular2 Beta版本进行迭代?

1 个解决方案

#1


2  

You can use the ng-for directive.

您可以使用ng-for指令。

Have a look at this. angular-2-ng-for-syntax

看看这个。角-2-纳克换语法

And you should convert your object to an array. Have a look here: a-better-way-to-convert-js-object-to-array

你应该将你的对象转换为数组。看看这里:a-better-way-to-convert-js-object-to-array

#1


2  

You can use the ng-for directive.

您可以使用ng-for指令。

Have a look at this. angular-2-ng-for-syntax

看看这个。角-2-纳克换语法

And you should convert your object to an array. Have a look here: a-better-way-to-convert-js-object-to-array

你应该将你的对象转换为数组。看看这里:a-better-way-to-convert-js-object-to-array