javascript - 一次又一次地使用 ng-repeat 可以吗?

标签 javascript angularjs json

我正在用 AngularJS 构建一个应用程序,我有一个非常复杂的 JSON 文件,其中有很多子数组和对象。所以我的问题是:为了从 JSON 访问数据,可以一次又一次地使用 ng-repeat 吗?

<div ng-repeat="parent in parents">
      <div ng-repeat="child in parent">
           <div ng-repeat="grandChild in child">
                {{grandChild.name}}
           </div>
      </div>
 </div>

----- 或者 AngularJS 中有一些循环方法可用

----- 或者我们必须使用旧的 JavaScript for 循环

示例数据

{"data":
    {
        "categories":
        {
            "articles":
            {
                "bdh":
                [
                    {"id":1, "name":"bdh article 1", "body":"this is bdh article 1 body."},
                    {"id":2, "name":"bdh article 2", "body":"this is bdh article 2 body."}
                ],
                "hadoop":
                [
                    {"id":3, "name":"hadoop article 1", "body":"this is hadoop article 1 body."},
                    {"id":4, "name":"hadoop article 2", "body":"this is hadoop article 2 body."}
                ]
            },
            "videos":
            {
                "bdh Videos":
                [
                    {"id":5, "name":"bdh videos 1", "body":"this is bdh videos 1 body."},
                    {"id":6, "name":"bdh videos 2", "body":"this is bdh videos 2 body."}
                ],
                "hadoop Videos":
                [
                    {"id":7, "name":"hadoop videos 1", "body":"this is hadoop videos 1 body."},
                    {"id":8, "name":"hadoop videos 2", "body":"this is hadoop videos 2 body."}
                ]
            }   
        }
    }
}

最佳答案

如果你有一个深层嵌套的结构,并且你想访问其中的每一个项目,那么是的,嵌套 ng-repeat 是非常好的。这就像将 for 循环嵌套在另一个循环中以访问对象的所有级别。

当然,如果您的对象非常大,那么您应该考虑不显示所有内容,但这适用于所有大对象,无论它们的信息是嵌套的还是平面结构。

关于javascript - 一次又一次地使用 ng-repeat 可以吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35101631/

相关文章:

javascript - MongoDB插入记录问题

javascript - Angular2 无法使用 javascript 导入 FormsModule

javascript - Angular - 从 CMS 延迟加载整个页面

身份验证后带有 restangular 注入(inject) header 的 Angularjs

c# - 简洁轻量的API : REST+JSON in . NET

java - 如何在 Spring MVC 中将正确的 JSON 传递给 Controller ​​?

javascript - 我需要 Javascript 函数来禁用回车键

javascript - 包含在框/表中的 jQuery.ripples

javascript - 使用 MathJax 获取 MathML 代码

python - 如何从 GCS 读取 json gzip 文件并写入表?