javascript - 如何使用 Angular JS 使用数组迭代 JSON 对象并填充表

标签 javascript angularjs json

我有一个 REST 服务,它返回类似于以下格式的 JSON 数据:

{
    "Africa" : [{
            "country" : "Nigeria",
            "capital" : "Abuja"
        }, {
            "country" : "Kenya",
            "capital" : "Nairobi"
        }, {
            "country" : "Ghana",
            "capital" : "Accra"
        }
    ],
    "Asia" : [{
            "country" : "India",
            "capital" : "New Delhi"
        }, {
            "country" : "China",
            "capital" : "Beijing"
        }
    ],
    "Europe" : [{
            "country" : "France",
            "capital" : "Paris"
        }
    ]
}

我应该如何解析这个结果并填充一个类似于下图的表格:

enter image description here

http://jsfiddle.net/p7yLztwg/

最佳答案

试试这个 Fiddle

<table ng-app="testApp" ng-controller="testController">
<tr>
  <td>Contitent</td>
  <td>Country</td>
  <td>Capital</td>
</tr>
<tr ng-repeat-start="(key, val) in testData">
    <td rowspan="{{val.length}}">{{key}}</td>
    <td>{{val[0].country}}</td>
    <td>{{val[0].capital}}</td>
</tr>
<tr ng-repeat-end ng-repeat="value in val.slice(1)">
    <td>{{value.country}}</td>
    <td>{{value.capital}}</td>
</tr>

关于javascript - 如何使用 Angular JS 使用数组迭代 JSON 对象并填充表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40386680/

相关文章:

javascript - 我的函数仅适用于我的 ajax 调用后的警报

javascript - 选择后模糊()/焦点()

Javascript 日期到字符串格式无效

angularjs - 真的值得混合使用 AngularJS 和 KendoUI 吗?

angularjs - require : 'ngModel' ? 是什么意思

javascript - Angular 1.5.4 $http 进度事件

javascript - 使用 jquery 获取 optgroup 中选择选项的索引

c# - 如何将嵌套的 JSON 保留为字符串

android - 如何使用 retrofit2 创建 json 对象数组的查询

javascript - 如何使用 d3 更新代表 SVG 行的所有数据?