简体   繁体   English

AngularJS和嵌套的JSON和ng-repeat

[英]Angularjs and nested JSON and ng-repeat

I have inherited code from another person who is no longer on the project, and I am still working on the paradigm shift to Angular alone (no one here to bounce ideas off of), so have a question for the group. 我已经从不再参与该项目的另一个人那里继承了代码,并且我仍在致力于将范式转移到Angular上(这里没有人可以摆脱想法),因此请小组成员提出一个问题。

I am getting JSON that has two nodes that I need to iterate over. 我正在获取具有两个需要迭代的节点的JSON。 The JSON is shown here: 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"
                }
            ]
        }
    ]
}

I need to display the questions.text and underneath it I need to display the questions.choices.display for each question. 我需要显示questions.text,并在其下方显示每个问题的questions.choices.display。 N number of questions with n number of answers. N个问题和n个答案。 Like so... 像这样

Example: 例:

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

What is the best/proper way to use ng-repeat in this case? 在这种情况下,使用ng-repeat的最佳/正确方法是什么? Should I have one array that contains the question and its answers and have ng-repeats over the parts that I need where I need them, or break it into two arrays (question and answers) and mix/nest the two? 我是否应该使用一个包含问题及其答案的数组,并对需要的部分进行ng-repeats,或者将其分为两个数组(问题和答案)并混合/嵌套两个? The latter seems possibly problematic. 后者似乎有问题。

Disclaimer: I am recovering from kidney stone surgery and on pain meds, so I'm having trouble visualizing the right answer. 免责声明:我正在从肾结石手术和止痛药中恢复过来,因此我很难想象正确的答案。 Please don't dog pile, I'm hurting and in a daze. 请不要乱堆,我很受伤,发呆。

I thank everyone in advance. 我预先感谢大家。

First off, don't apologize at all. 首先,不要道歉。 Hope you have a quick recovery. 希望您早日康复。

Now, about your question, I've done similar things using a nested ng-repeat approach. 现在,关于您的问题,我使用嵌套的ng-repeat方法做了类似的事情。

<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>

Of course you would use your custom directives and whatnot. 当然,您将使用自定义指令等。 Best of luck in your project. 祝您项目顺利。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM