javascript - AngularJS 多个嵌套 ng-repeats

标签 javascript angularjs angularjs-ng-repeat

当我有 json 时,我经常做一个嵌套的 ng-repeat 来循环子数据,例如

[
 { "accountnum": 1,
   "name": "foo",
   "subacct": [
      { "accountnum": 1-1,
        "name": "bar"
      } ...
    ]
  } ...
]

然后我使用这个模式:

     <tr ng-repeat-start="a in cac.costAccounts">
          <td>{{a.first_name}} {{a.last_name}}</td>
          <td>{{a.account_number}}</td>
          <td>{{a.description}}</td>
        </tr>
        <tr ng-repeat-end ng-repeat-start="sa in a.subacct">
          <td>{{sa.first_name}} {{sa.last_name}}</td>
          <td>{{sa.account_number}}</td>
          <td>{{sa.description}}</td>
        </tr>

这有效。但是这次数据包含第三个级别; subacct 对象的键为 subsubacct

[
 { "accountnum": 1,
   "name": "foo",
   "subacct": [
      { "accountnum": 1-1,
        "name": "bar",
        "subsubacct": [
        { "accountnum": 1-1-1,
          "name": "bar"
        } ...
      } ...
    ]
  } ...
]

所以我尝试添加第三个级别,但它没有显示(没有控制台错误):

        <tr ng-repeat-start="a in cac.costAccounts">
          <td>{{a.first_name}} {{a.last_name}}</td>
          <td>{{a.account_number}}</td>
          <td>{{a.description}}</td>
        </tr>
        <tr ng-repeat-end ng-repeat-start="sa in a.subacct">
          <td>{{sa.first_name}} {{sa.last_name}}</td>
          <td>{{sa.account_number}}</td>
          <td>{{sa.description}}</td>
        </tr>
        <tr ng-repeat-end ng-repeat="ssa in sa.subsubacct">
          <td>{{ssa.first_name}} {{ssa.last_name}}</td>
          <td>{{ssa.account_number}}</td>
          <td>{{ssa.description}}</td>
        </tr>

那么如何获得第三个循环?

最佳答案

一种解决方案是使用 tbody

<tbody ng-repeat="a in cac.costAccounts">
    <tr>
      <td>{{a.first_name}} {{a.last_name}}</td>
      <td>{{a.account_number}}</td>
      <td>{{a.description}}</td>
    </tr>
    <tr ng-repeat-start="sa in a.subacct">
      <td>{{sa.first_name}} {{sa.last_name}}</td>
      <td>{{sa.account_number}}</td>
      <td>{{sa.description}}</td>
    </tr>
    <tr ng-repeat-end="ssa in sa.subsubacct">
      <td>{{ssa.first_name}} {{ssa.last_name}}</td>
      <td>{{ssa.account_number}}</td>
      <td>{{ssa.description}}</td>
    </tr>
</tbody>

关于javascript - AngularJS 多个嵌套 ng-repeats,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43939304/

相关文章:

javascript - 客户端如何使用CORS跨域?

javascript - 解析 JSON 中的特定值以进行 ajax

javascript - 如何检查 html 表中的 hasClass

javascript - Angular ng-repeat 导致闪烁

angularjs - 在两个数组上进行 ng-repeat

AngularJS "Vertical"ng-repeat

javascript - jQuery Ajax POST 并返回数据

javascript - Angular.js 搜索查询(如果搜索表单不在当前文件中)

javascript - 如何使用 Angularjs 重新加载页面?

javascript - Angular 过滤器范围缩小至 1 个关键类型以准备发送