我有一个名为 current_item.get_user_history()
的 JS 函数,它通过进行 API 调用返回一个数组,看起来类似于以下内容:
things[0]:
thing_id: 5
user_id: 23
paddle_no: 1234
item_id: 893
price: 5000
things[1]:
thing_id: 4
user_id: 67
paddle_no: 6743
item_id: 893
price: 4500
... and so on
我希望能够使用 ng-repeat 从此数组中获取数据来填充表。
<div class="bid_history">
<table>
<tr>
<th>
Column 1
</th>
<th>
Column 2
</th>
<th>
Column 3
</th>
</tr>
<tr ng-repeat="thing in current_item.get_user_history() track by thing.id">
{{thing.user_id}} {{thing.price}}
</tr>
</table>
</div>
出于某种原因,没有任何内容被渲染,而且似乎做了很多重复,因为我在 chrome 控制台中收到了不可阻挡的大量错误。如果您能准确解释如何使用 ng-repeat ,我们将不胜感激。
最佳答案
您不能使用触发 $digest()
的函数(如 $http
、 $timeout
)在 ng-repeat
中。它会导致无限$digest()
环形。
有说明:
https://github.com/angular/angular.js/issues/705或angular Infinite $digest Loop in ng-repeat .
我以前也犯过同样的错误:)
Infinite loop when ng-repeat/ng-class calls a function which calls $http
您必须保存current_item.get_user_history()
先数据,然后使用ng-repeat
调用数据。
scope.things= urrent_item.get_user_history();
<tr ng-repeat="thing in things track by thing.id">
关于javascript - 使用 ng-repeat 用数组中的数据填充表行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36226766/