如何禁用在AngularJS中使用ng-repeat创建的按钮子集?

时间:2014-10-04 21:10:07

标签: javascript angularjs

我正在尝试使用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-disabled='true'功能中设置ng-click='submitOption()',则会禁用所有问题的所有选项。我怎样才能在进行测验时一次禁用一个问题的选项?

我一直在考虑将数据(question.q)从第一个ng-repeat范围传递到嵌套的ng-repeat,并仅将属性ng-disabled='true'添加到带有ng-data='{{question.q}}'的选项按钮。< / p>

1 个答案:

答案 0 :(得分:1)

尝试在测验中向问题添加submitted属性,然后将submitOption更改为question

submitOption(option.value, question)

submitOption中,您可以访问question.answerquestion.q并将question.submitted设置为true

然后,您可以使用ng-disabled='question.submitted'禁用该问题的选项。