Good Morning!
I've found several posts on stack that touch on this, but I haven't found any that accomplish what I'm trying to do exactly.
(JS Fiddle for Reference: http://jsfiddle.net/gsLXf/1/)
I have a dynamic set of questions that I am asking as part of a survey. I have figured out how to bind the responses in text and radio responses to a 'response' object that I can post back to my server. The problem I am having is with Checkboxes. As you can see in the fiddle, when I try to do
ng-model="response[question.id]"
all of my checkboxes respond as one item (which makes sense since they are all bound to the same value. However, when I use
ng-model="response[question.id][option.id]"
I pop an error because question.id hasn't been instantiated yet so option.id can't be pushed onto the object.
Ideally my response object for the referenced fiddle would look like this:
{
"123456": "2", //radio question
"789012": //checkbox question
["5", "6"] //list of checkbox options ids selected
}
My users will be creating forms dynamically, so I have to be able to handle this very gracefully in all situations. I can't hard-code ANY object related data into a controller and I can't hand-create model objects to handle this situation.
I have considered looping through the known ID set to scaffold a response object to be filled during initialization, but it seems like overkill when Angular has such a nice way of just creating a response object on the fly (except for this situation).
What is the best way to go about doing this?