2
\$\begingroup\$

Is there a better way, or the correct one to have this approach for a JSON object that hast the following structure? (it's only a portion of my object, the most important one)

{
    "Questions": [
        {
            "AnswerType": "single-choice",
            "Answers": [
                {
                    "AnswerValue": null,
                    "Description": "Yes",
                    "ID": 124877,
                    "Questions": [
                        {
                            "AnswerType": "multiline",
                            "Answers": [
                                {
                                    "AnswerValue": null,
                                    "Description": null,
                                    "ID": 124879,
                                    "Questions": []
                                }
                            ],
                            "Description": "Other text",
                            "ID": 37932,
                            "SelectedAnswers": []
                        },
                        {
                            "AnswerType": "singleline",
                            "Answers": [
                                {
                                    "AnswerValue": null,
                                    "Description": null,
                                    "ID": 124880,
                                    "Questions": []
                                }
                            ],
                            "Description": "Hoeveelperweek?",
                            "ID": 37933,
                            "SelectedAnswers": []
                        }
                    ]
                },
                {
                    "AnswerValue": null,
                    "Description": "No",
                    "ID": 124878,
                    "Questions": []
                }
            ],
            "Description": "Question text",
            "ID": 37931,
            "SelectedAnswers": []
        }
    ]
}

AngularJS template for parsing it recursively:

<div ng-repeat="question in myData.Questions">
    {{question.Description}}
    <div ng-repeat="answer in myData.Questions[$index].Answers">
        <a href="#" ng-click="answerQuestion(answer)">{{answer.Description}}</a>
        <div ng-repeat="qanswer in myData.Questions[$index].Answers[$index].Questions">
            <a href="#" ng-click="answerQuestion(qanswer)">{{qanswer.Description}}</a>
        </div>
    </div>
</div>

The structure can go infinitely. This is the structure that is being repeated over and over:

Questions []
-Answers []
--Answer [i]
---Questions []

JSBin example is here

\$\endgroup\$
2
  • 1
    \$\begingroup\$ I presume you want to use a custom Angular directive recursively, i.e. calling itself in its template. \$\endgroup\$ Commented Apr 11, 2015 at 15:54
  • \$\begingroup\$ Have you tried SO - that may be better suited for question "how to ..." as opposed to review your code. \$\endgroup\$ Commented Apr 11, 2015 at 15:57

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.