嵌套的ng-repeat并没有正常工作

时间:2016-01-14 23:30:46

标签: javascript angularjs

我试图调用一个双嵌套的ng-repeat来循环遍历对象中的对象,它只是不起作用。我正在使用表格(tr,td)

这是我的HTML代码:

<tr ng-repeat="row in rows">
  <td ng-repeat="item in row">
     {{item}}
  </td>
</tr>

这是我的数据对象:

$scope.rows = [["10:00", "0"],["12:00","1"]]

但桌子上没有任何东西可以打印。我做错了什么?谢谢!

更新

stackoverflow示例工作正常,抱歉。

使用我的数据原始数组不起作用:

[["10:00","0","0","3","0","0","0","0"],    
["12:00","0","0","3","0","0","0","0"], 
["14:00","0","0","3","0","0","0","0"],
["16:00","0","0","3","0","0","0","0"],
["18:00","0","0","3","0","0","0","0"],
["20:00","0","0","3","0","0","0","0"],
["22:00","0","0","3","0","0","0","0"]]

我想创建类似的东西:

enter image description here

1 个答案:

答案 0 :(得分:1)

使用完全如图所示的示例数据运行代码时,控制台日志中会记录以下错误:

  

Error: [ngRepeat:dupes] Duplicates in a repeater are not allowed. Use 'track by' expression to specify unique keys. Repeater: item in row, Duplicate key: string:0, Duplicate value: 0

可以通过向track by添加ng-repeat表达式来解决此问题。由于数据中没有特定列可用作索引,因此您可以使用$index的{​​{1}}特殊属性。

ng-repeat

此代码将为您提供预期的输出。

http://plnkr.co/edit/aSbaom85JsQwUfTIwKk3?p=preview