Hey there!
I've been searching on Stack Overflow regarding this topic, but I haven't found a solution that matches exactly what I need.
If you want to take a look at the code, here is a JSFiddle link for reference: http://jsfiddle.net/gsLXf/1/
Currently, I'm working on a survey with a dynamic set of questions. I have managed to bind text and radio responses to a 'response' object successfully. However, I'm facing an issue with checkboxes. In the fiddle example, when I use
ng-model="response[question.id]"
all checkboxes act as one entity since they are bound to the same value. On the other hand, if I try using
ng-model="response[question.id][option.id]"
I encounter an error because question.id hasn't been initialized yet, therefore option.id can't be added to the object.
The response object in the JSFiddle should ideally look like this:
{
"123456": "2", //radio question
"789012": //checkbox question
["5", "6"] //list of selected checkbox option ids
}
Since my users will be creating forms dynamically, it's crucial for me to handle this gracefully under all circumstances. I cannot manually input any object-related data into a controller or create model objects specifically for this scenario.
I have thought about looping through the known ID set to structure a response object to be filled during initialization, but it feels like too much work considering Angular offers a simple way of creating response objects (except in this case).
What would be the most effective approach to deal with this situation?