【发布时间】:2014-05-30 06:00:18
【问题描述】:
我是 Angular JS 的新手,我正在将 JSON 绑定到我的 HTML:
{
"CompanyName": "Company Ltd.",
"Products": [
{
"ProductId": 1245,
"Name": "Trial",
"ProductItems": [
{
"ProductId": 1254,
"ProductItemName": "Service 1",
"ProductItemType": "Service"
},
{
"ProductId": 7789,
"ProductItemName": "Service 2",
"ProductItemType": "Service"
}
]
}
]
}
我的 HTML 是一个基本上如下所示的表格:
<table ng-controller="ProductController as productCtrl">
<thead>
<tr>
<th id="CompanyName">
<h1 class="h1">{{productCtrl.data.CompanyName}}</h1>
</th>
<th ng-repeat="product in productCtrl.data.Products">
<h2>{{product.Name}}</h2>
<h3>
<span>{{product.ProductPrice}}</span>
<span> {{product.CurrencySymbol}} / {{product.PriceTimePeriod}} </span>
</h3>
</th>
</tr>
</thead>
<tbody>
<!-- Need `product` here so that I can loop on their ProductItems -->
<tr ng-repeat="item in product.ProductItems">
<td>{{item.ProductItemName}}</td>
<td>{{item.Amount}}</td>
</tr>
</tbody>
<tfoot>
</tfoot>
</table>
Foreach Product in Products 我在thead 和Item in ProductItems of each Product 中生成一个列@ 正确。
关于如何克服这个问题的任何建议?
【问题讨论】:
-
“Foreach Product in Products 我在前面生成一行” - 你的意思是为每个产品创建一列(不是一行)。在您提供的代码中,它为每个产品创建了一个列标题。
-
@guru 是的,是的。对不起那个错字。
-
代码中的header列和body列不匹配。您是否想做这样的事情 - plnkr.co/edit/64kByaQ8DYm2wvpTgBeR?p=preview
-
@guru 是的,没错。类似的东西。我只需要弄清楚范围界定的东西。
标签: javascript angularjs