在包含JSON的JSON上使用ng-repeat

Stu*_*Stu 21 angularjs angularjs-ng-repeat

我对角度有点新,而且我的json和ng-repeats也有问题.我有一个"模块"列表,然后列出"周":

{
    "modules":
        {
            "module1":
                {
                   "title":"name of module1",
                   "description":"description of module1",
                   "weeks":{"week1":{"title":"Week 01"}
                },
            "module2":
                {
                   "title":"name of module2",
                   "description":"description of module2",
                   "weeks":{"week2":{"title":"Week 02"},"week3":{"title":"Week 03"}
                }
        }
}
Run Code Online (Sandbox Code Playgroud)

我的最终输出是一个表,我可以让模块重复,但是我很难理解我做错了几个星期循环.这是我的模板:

<table class="table table-bordered" ng-repeat="module in ocw.modules">
<tr>
    <td>
        <h3 class="moduletitle">{{ module.title }}</h3>
        <h4>Description</h4>
        <p>{{ module.description }}</p>
    </td>
</tr>
<tr ng-repeat="week in ocw.modules.weeks">
    <td>
        {{ week.title }}
    </td>
</tr>
</table>
Run Code Online (Sandbox Code Playgroud)

所以这将输出2个表,具有正确的标题和描述,但我似乎无法正确显示几周.请注意,一些"模块"有一个"周".我不确定错误是在我的模板还是json中.

谢谢你的帮助.小号

luc*_*uma 39

我会改变你的数据结构,所以你的模块和周是一个对象数组.

演示:http://plnkr.co/edit/e41n9vAMMLf0WWIUQ8HO?p = preview

json数据:

{
    "modules":
        [
                {
                   "title":"name of module1",
                   "description":"description of module1",
                   "weeks":[{"id":1, "title":"Week 01"}]
                },

                {
                   "title":"name of module2",
                   "description":"description of module2",
                   "weeks":[{"id":2, "title":"Week 02"},{"id":3,"title":"Week 03"}]
                }
        ]
}
Run Code Online (Sandbox Code Playgroud)

然后你的标记将是:

<table class="table table-bordered" ng-repeat="module in ocw.modules">
<tr>
    <td>
        <h3 class="moduletitle">{{ module.title }}</h3>
        <h4>Description</h4>
        <p>{{ module.description }}</p>
    </td>
</tr>
<tr ng-repeat="week in module.weeks">
    <td>
        {{ week.title }}
    </td>
</tr>
</table>
Run Code Online (Sandbox Code Playgroud)

当你在每个模块上进行迭代时,在这种情况下是module为了得到几周,它就module.weeks像是一样module.title.在您的示例中,您在迭代内部并尝试引用ocw.modules.weeks与您的json结构不匹配的内容.