简体   繁体   中英

Setting ng-model inside ng-repeat not working

I have a list of questions and each question have some options with checkbox. I'm using an ng-repeat to list the questions and another ng-repeat to list the options. So how should I set the ng-model for the answer option checkbox to get all the selected options for all questions.

I tried setting ng-model like this answers[qst.id][ans.id] , But its returning error TypeError: Cannot set property '*' of undefined

<div ng-repeat="qst in questions">
    <div>{{qst.question}}</div>
    <div>
        <ul>
            <li ng-repeat="ans in answers">
                <span>
                    <input type="checkbox" ng-model="answers[qst.id][ans.id]">
                </span>
                <span>
                    {{ans.ansOption}}
                </span>
            </li>
        </ul>
    </div>
</div>

What is the perfect way to do that ?

Dont know your structure of Answers, but it is probably object (key-value pair), so i binded your checkbox to key checked in answer.

<div ng-repeat="qst in questions">
    <div>{{qst.question}}</div>
    <div>
        <ul>
            <li ng-repeat="ans in answers[qst.id]"><span><input type="checkbox" ng-model="ans.checked"></span><span>{{ans.ansOption}}</span></li>
        </ul>
    </div>
</div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM