我正在尝试使用 AngularJS 构建一个测验 Web 应用程序。测验问题的结构如下:
$scope.quiz = [
{
'q': 'For what period would archaeologists first begin to find permanent human settlements?',
'options': [{ 'value': 'The Paleolithic'} , { 'value': 'The Classical Era'} , {'value' : 'The Bronze Age'} , { 'value': 'The Neolithic'}],
'answer': 'The Neolithic',
'difficulty': 2
},
{
'q': 'Which of the following civilizations shares the most in common with the Harappan civilization found in the Indus River Valley?',
'options':[{ 'value': 'The Mogul Empire in India'} , { 'value': 'The Tokugawa Shogunate in Japan'} , {'value' : 'The Olmec civilization in Mesoamerica'} , { 'value': 'The Athenian city- state in ancient Greece'}],
'answer': 'The Olmec civilization in Mesoamerica',
'difficulty': 6
},
{
'q': 'Identify the important original contribution of the Hebrew culture to the civilizations in the Middle East and Mediterranean during the Classical Period.',
'options':[{ 'value': 'Monotheism'} ,{ 'value': 'Written legal code'} , {'value' : 'Phonetic alphabet'} , { 'value': 'Priest caste'}],
'answer': 'Monotheism',
'difficulty': 5
},
{
'q': 'Confucianism established political and social systems in China while what other philosophy contributed significantly to China’s medical practices and art and architecture?',
'options':[{ 'value': 'Legalism'} ,{ 'value': 'Shintoism'} , {'value' : 'Hinduism'} , { 'value': 'Daoism'}],
'answer':'Daoism',
'difficulty': 3
}
];
我使用嵌套的 ng-repeat 来显示问题和每个问题的选项:
<div class='question' ng-repeat='question in quiz | orderBy: difficulty' value='{{$index}}'>
<h3>{{$index+1}}. {{question.q}}</h3>
<button type='button' class='btn btn-default btn-block' ng-repeat='option in question.options' ng-click='submitOption(option.value, question.answer, question.q)' ng-disabled='' ng-data='{{question.q}}'>
<input type='checkbox'> <strong>{{option.value}}</strong>
</button>
</div>
当我点击一个选项时,我希望禁用该问题的所有选项,但如果我只是在 ng-click= 中设置
函数,所有问题的所有选项都将被禁用。如何才能在进行测验时一次禁用一个问题的选项?ng-disabled='true'
'submitOption()'
我一直在考虑将数据(question.q)从第一个 ng-repeat 范围传递到嵌套的 ng-repeat 并将属性 ng-disabled='true'
仅添加到选项带有 ng-data='{{question.q}}'
的按钮。
最佳答案
尝试向测验中的问题添加 subscribed
属性,然后更改 submitOption
以接受问题
:
submitOption(选项.值, 问题)
在submitOption
中,您可以访问question.answer
和question.q
并将question.subscribed
设置为真
。
然后,您可以使用 ng-disabled='question.subscribed'
仅禁用该问题的选项。
关于javascript - 如何禁用在 AngularJS 中使用 ng-repeat 创建的按钮子集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26197206/