动态创建单选按钮并选择适当的按钮Angularjs

时间:2016-01-27 20:21:02

标签: javascript html angularjs

我有一个问卷表单,循环问题和问题中的答案,然后创建带答案的单选按钮。答案对象将具有已回答的属性,该属性将为真或假,具体取决于先前是否已为该项目选择了该答案。如果answer.answered = true,则应检查该单选按钮(只有一个已被回答)。问题是我无法根据answer.answered检查相应的单选按钮。我试过ng-value =“answer.answered”,在init上设置模型。以下缩写代码。

 <md-card data-ng-repeat="question in gradeCtrl.questions | limitTo: 1  track by $index" class="questions main-questions" 
data-ng-if="!gradeCtrl.subQuestions && gradeCtrl.needsRepairs === false">
<ul data-ng-repeat="answer in question.answers track by $index" class="questionnaire" layout-wrap layout="column" layout-align="center center">
<li>
<input type="radio" name="{{question.id}}" data-ng-attr-id="answer.id" data-ng-value="gradeCtrl.isAnswered(answer);"
 data-ng-init="gradeCtrl.radioButton[answer.id]=gradeCtrl.isAnswered(answer);"
 data-ng-model="gradeCtrl.radioButton[answer.id]">
</li>
</md-card>

1 个答案:

答案 0 :(得分:0)

我能够通过设置model = answer.answered以及使用ng-checked来解决它。

<input type="radio" name="{{question.id}}"  data-ng-checked="gradeCtrl.isAnswered(answer);"
                       data-ng-attr-id="answer.id"
                       data-ng-init="gradeCtrl.radioButton[answer.id] = gradeCtrl.isAnswered(answer);"
                       data-ng-click="gradeCtrl.saveAnswer(answer, question.question)"
                       data-ng-model="answer.answered">