0

I am trying to make my ng-model dynamic, when i use only one level object ng-model="data[question.option_group_id]" it works fine. However when try to use 2 level object structure ng-model="data[page.id][question.option_group_id]" it gives me error: TypeError: Cannot set property '10' of undefined.

<div class="radio" ng-repeat="option in question.option_group.option_choices">
    <label>
        <input type="radio" ng-model="data[page.id][question.option_group_id]" value="{{ option.id }}" name="{{ question.option_group_id }}">
        {{ option.name }}
    </label>
</div>

Any ideas how can this be solved ?

1
  • Could you create a fiddle/plunker to see how it is defined in your scope please? Commented Feb 10, 2014 at 10:47

1 Answer 1

1

Problem is data[page.id][question.option_group_id]. This will throw the error you are seeing unless you define data[page.id] first. You can navigate to data[page.id][question.option_group_id] only if data[page.id] has been defined.

Sign up to request clarification or add additional context in comments.

4 Comments

I see, is there a way to define data[page.id] it in the view ?
You cannot define data[page.id] in the view. You have to do it in the controller.
Yes that's what I did eventually. Thanks for help.
I think you can try <div class="radio" ng-repeat="option in question.option_group.option_choices" ng-init='data[page.id]={}'>

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.