【问题标题】:Cannot access inner Array of objects using ng-repeat无法使用 ng-repeat 访问内部对象数组
【发布时间】:2016-01-10 18:09:42
【问题描述】:

所以我有这个数组,其中包含有关父页面和子页面的信息。我正在尝试使用 ng-repeat 将其打印出来,但我似乎只获得了第一级数据。我似乎无法访问内部子数据。感激地接受任何帮助

这是我到目前为止所尝试的

<div ng-repeat="page in pages" >{{page.post_title}}
            <div ng-repeat="child in page.children">{{child.post_title}}</div>
        </div>

$scope.pages=[
{
    "id": "1",
    "post_title": "Batman Ipsum",
    "publish_date": "Monday, October 12th 2015, 9:35:16 pm",
    "is_draft": "0",
    "children": [
        {
            "id": "7",
            "post_title": "TV Ipsum",
            "publish_date": "Monday, October 12th 2015, 11:44:24 pm",
            "is_draft": "0",
            "children": [
                {
                    "id": "6",
                    "post_title": "Yet another Batman Ipsum",
                    "publish_date": "Thursday, October 22nd 2015, 11:00:00 pm",
                    "is_draft": "0"
                }
            ]
        }
    ]
},
{
    "id": "2",
    "post_title": "Title fit for a (precariously enthroned) king",
    "publish_date": "Monday, October 12th 2015, 10:07:01 pm",
    "is_draft": "0"
},
{
    "id": "3",
    "post_title": "Cupcake Ipsum",
    "publish_date": "Monday, October 12th 2015, 10:13:23 pm",
    "is_draft": "0"
},
{
    "id": "4",
    "post_title": "The most presidential lorem ipsum in history.",
    "publish_date": "Monday, October 12th 2015, 10:27:15 pm",
    "is_draft": "0"
},
{
    "id": "5",
    "post_title": "Quote Ipsum",
    "publish_date": "Null",
    "is_draft": "1"
}
]

但我得到的结果是

Batman Ipsum
TV Ipsum
Title fit for a (precariously enthroned) king
Cupcake Ipsum
The most presidential lorem ipsum in history.
Quote Ipsum

没有一个孩子被打印出来

【问题讨论】:

    标签: javascript json angularjs multidimensional-array angularjs-ng-repeat


    【解决方案1】:

    您还需要一个&lt;div ng-repeat="item in child.children"&gt;&lt;/div&gt; 才能访问孩子父母的孩子!

    或者如果您的最后一个孩子总是一个,您可以通过child[0].id访问他们

    【讨论】:

      猜你喜欢
      • 2015-03-20
      • 2015-03-16
      • 2014-08-18
      • 1970-01-01
      • 1970-01-01
      • 2016-06-17
      • 2017-08-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多