Angularjs和嵌套的JSON和ng重复

Angularjs and nested JSON and ng-repeat

本文关键字:ng 重复 JSON 嵌套 Angularjs      更新时间:2023-09-26

我从另一个不再参与该项目的人那里继承了代码,我仍在独自进行向Angular的范式转换(这里没有人可以提出想法),所以有一个问题要问小组。

我得到的JSON有两个节点需要迭代。JSON如下所示:

{
    "questions": [
        {
            "text": "Do you plan to attend the session on 6/3 at 12?",
            "choices": [
                {
                    "name": "Answer_9LUL3A",
                    "display": "Yes",
                    "value": "Yes"
                },
                {
                    "name": "Answer_9LUL3A",
                    "display": "No",
                    "value": "No"
                }
            ]
        },
        {
            "text": "When you get the meeting/appointment invite accept it with the Notes client (as opposed to the iDevice)",
            "choices": [
                {
                    "name": "Answer_9NFPMZ",
                    "display": "September 1, 2014 12:00 PM - 01:00 PM",
                    "value": "09/01/2014~12:00:00 PM~01:00:00 PM"
                },
                {
                    "name": "Answer_9NFPMZ",
                    "display": "September 1, 2014 01:00 PM - 02:00 PM",
                    "value": "09/01/2014~01:00:00 PM~02:00:00 PM"
                },
                {
                    "name": "Answer_9NFPMZ",
                    "display": "September 1, 2014 02:00 PM - 03:00 PM",
                    "value": "09/01/2014~02:00:00 PM~03:00:00 PM"
                },
                {
                    "name": "Answer_9NFPMZ",
                    "display": "September 1, 2014 03:00 PM - 04:00 PM",
                    "value": "09/01/2014~03:00:00 PM~04:00:00 PM"
                }
            ]
        }
    ]
}

我需要显示问题文本,在它下面我需要显示每个问题的问题选项显示。N个问题,N个答案。就像这样…

示例:

Q1 How are you?
A1 Feelin' Fine
A2 Fair
A3 Feelin' Bad
Q1 Where are you?
A1 Beach in Bahamas
A2 Broadway in New York
A3 Prison in Leavenworth

在这种情况下,使用ng repeat的最佳/正确方法是什么?我应该有一个包含问题及其答案的数组,并在我需要的部分重复ng次,还是将其分解为两个数组(问题和答案),并将两者混合/嵌套?后者似乎有问题。

免责声明:我正在从肾结石手术和止痛药中恢复,所以我很难想象正确的答案。请不要乱堆,我又疼又迷。

我提前感谢大家。

首先,不要道歉。希望你早日康复。

现在,关于你的问题,我已经使用嵌套ng重复方法做了类似的事情。

<div ng-controller='QuestionsController as questionList'>
    <div ng-repeat='question in questionList.questions'>
        {{ question.text }}
        <div ng-repeat='choice in question.choices'>
            {{ choice.display }}
        </div>
    </div>
</div>

当然,你会使用你的自定义指令等等。祝你项目顺利。