角度嵌套的ng-repeat表

时间:2014-11-06 23:27:39

标签: angularjs angularjs-ng-repeat

给定TableController中的数据结构:

var obj = {
  titles : { 
     title: 'Title'
  ,  data : ['a title' , 'another title', 'look a title', 'and another']
  }
, other-prop : { 
     title: 'Other Prop'
  ,  data : [3030, 849875, 8888, 48484]
  }
}

this.obj = obj;

如果你的目标是创建一个像这样的表:

table
  thead
    tr
      th "Title"
      th "Other Prop"

  tbody
    tr
      td "a title"
      td "3030"
    tr
      td "another title"
      td "849875"
    tr
      td "look a title"
      td "8888"
    tr
      td "and another"
      td "48484"

你将如何构建ng-repeats?现在我有:

div [controller="TableController as table"]

table
  thead
    tr 
      th [ng-repeat = "(key, value) in table.obj"]

  tbody
    tr [ng-repeat = "(key, value) in table.obj"
      td [ng-repeat="(key, val) in table.obj track by $index"] "{{value.data}}"

哪位给了我......

table
  thead 
    tr
      th "Title"
      th "Other Prop"
  tbody
    tr
      td "['a title' , 'another title', 'look a title', 'and another']"
      td "[3030, 849875, 8888, 48484]"
    tr
      td "['a title' , 'another title', 'look a title', 'and another']"
      td "[3030, 849875, 8888, 48484]"

...所以我关闭了,但是我无法得到......以正确的方向列出的对象。

任何?

1 个答案:

答案 0 :(得分:1)

您可以直接索引到table['other-prop'].data数组:

div [controller="TableController as table"]

table
    thead
        tr 
            th [ng-repeat = "(key, value) in table.obj"]

    tbody
        tr [ng-repeat = "(key, value) in table.obj.data"
            td "{{value}}"
            td "{{table['other-prop'].data[$index]}}"

它可以正常工作,但不能扩展到任意数量的列表。

为此,您需要采用Brian Noah的建议,并将数据预处理为更容易呈现的结构。