Creating a nested form from a JSON object called formObject, I bind the values within the object itself. By recursively parsing the values, I extract the actual data, referred to as dataObject, upon submission.
To view the dataObject in a linear format, refer to this example: http://jsfiddle.net/DrQ77/80/.
<select ng-model="Answers[question.Name]" ng-options="option for option in question.Options">
In contrast, you can also see an example of recursion here: http://jsfiddle.net/DrQ77/92/. The variable question
has been renamed to element
to represent both questions and sections. Each section may contain multiple questions and subsections, demonstrating nesting levels.
Answers=[{
section:"Personal",
values:[{GenderQuestion:"Male"},{MaritalStatus:"Married"},{section:"Sub Personal",values:[{LivingWith:"Alone"}]}]
}, {
section:"Random",
values:[{ColorQuestion:"Red"}],
},
{SectionLess:"opt1"}]
This approach allows for nesting at various levels, which may not be achievable with $scope.Answers from the first example. However, when updating an existing dataObject, there is a need to map the dataObjects onto the formObject before rendering, then parse it again upon submission. While effective, this method lacks elegance due to its recursive nature, making me wonder if there is a more 'angular way' to accomplish this.
Has anyone successfully implemented a similar solution in a more efficient manner? How can I improve this process?