我正在嘗試構建一個包含動態列數的表,並希望使用這些列標題來決定要顯示哪個對象的屬性。以下是我想要做的事情,但它會拋出一個解析錯誤。有任何想法嗎?使用一個ng-repeat中的值作爲另一個ng-repeat的一部分
<table class="table">
<thead>
<th ng-repeat="th in tableHeaders">{{th}}</th>
</thead>
<tr ng-repeat="item in items | filter:query | orderBy:orderProp">
<td>{{item.{{th}}}}</td>
</tr>
</table>
個陣列
['column1', 'column2']
項數組
[
{column1:blah, column2:blah},
{column1:blah2, column2:blah2}
]
更新1 我嘗試使用以下,我得到一堆空行的。
<tr ng-repeat="item in items | filter:query | orderBy:orderProp">
<td ng-repeat="th in tableHeaders">{{item.$eval(th)}}</td>
</tr>
你可以發佈樣本數據嗎?你想達到什麼目標? – Satpal
'ng-repeat'創建它自己的子範圍,所以'th'不能在其他'repeat'中訪問,因爲它超出了範圍。 – tymeJV
'th'變量只存在於它自己的'scope'中的'ng-repeat'塊中。在第二個ng-repeat中,你可以使用'tableHeaders [$ index]'而不是'th'。但是,正如我所看到的,您正試圖訪問項目的屬性,因此可能需要一個eval()? – Animator