【问题标题】:Iterating objects or arrays inside ng-repeat在 ng-repeat 中迭代对象或数组
【发布时间】:2013-09-03 17:00:33
【问题描述】:

我传递到我的视图中的 JSON 在其中包含对象和数组。示例:

{ name: "test", projects: [ { name: "project1", tasks: "task1" } ] }

使用 ng-repeat,我可以说 (key, value) in items 并得到你所期望的,每个键和对象或数组的字符串化版本。

是否存在迭代 ng-repeat 中的对象和数组的“最佳实践”?

【问题讨论】:

  • 这取决于您将如何显示数据。

标签: angularjs angularjs-ng-repeat


【解决方案1】:

只是跟进一个答案,因为我能够弄清楚这一点。这比我意识到的要简单。刚刚对数组项使用了另一个 ng-repeat。

<div class="container" ng-repeat="item in items">
<table class="table table-striped table-bordered table-condensed">
    <tr>
        <th class="span3">name</th>
        <td>{{ item.name }}</td>
    </tr>
    <tr>
        <th class="span3">accounts</th>
        <td>
            <ul ng-repeat="account in item.accounts">
                <li>username: {{ account.username }}</li>
                <li>password: {{ account.password }}</li>
            </ul>
        </td>
    </tr>
</table>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-12
    • 1970-01-01
    • 2014-03-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多