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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34762821/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 03:12:56  来源:igfitidea点击:

How to iterate JSON object in Angular 2?

angulartypescriptloops

提问by Zeyar Phyo Maung

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 Betaversion?

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

回答by harmoniemand

You can use the ng-for directive.

您可以使用 ng-for 指令。

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

看看这个。 angular-2-ng-for-语法

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