javascript - 使用 ng-repeat 用数组中的数据填充表行

标签 javascript angularjs

我有一个名为 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/705angular 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/

相关文章:

javascript - Angularjs 向指令添加样式(使用 Css 文件)

angularjs - 我可以在浏览器中使用 amqplib 吗?

java - 使用用 Java 编写的现有 Selenium 测试的 AngularJs 端到端测试

javascript - kendo-ui 数字文本框不工作步长值 0.001

javascript - focus() 函数在 Firefox 中不起作用

javascript - 无法访问 JSON 数组中的元素

javascript - 似乎无法让 ngAnimate 在 ng-repeat 上工作

javascript - 使用 track by $index 时,ng-repeat 中的自定义指令不会正确更新绑定(bind)值

javascript - 是否可以使用 new option() 将 ondblclick 添加到选项字段?

javascript 绑定(bind)函数不绑定(bind)值( Electron 远程回调)