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

标签 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-click= 中设置 ng-disabled='true' 'submitOption()' 函数,所有问题的所有选项都将被禁用。如何才能在进行测验时一次禁用一个问题的选项?

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

最佳答案

尝试向测验中的问题添加 subscribed 属性,然后更改 submitOption 以接受问题:

submitOption(选项.值, 问题)

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

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

关于javascript - 如何禁用在 AngularJS 中使用 ng-repeat 创建的按钮子集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26197206/

相关文章:

javascript - knockout 条件绑定(bind)(但不是 native "if"方式)

javascript - 未捕获的类型错误 : Cannot read property 'then' of undefined when chaining 3 getJson calls

javascript - 在AngularJS中同时显示内容

javascript - SOS,$scope 不适用于 algular js

angularjs - 从模板( View )中动态加载 AngularJS 模块

javascript - 指令的范围与其模板之间没有数据绑定(bind)

javascript - Bokeh 自定义保存工具

没有包含 CSS 文件?

javascript - 将 ng-options 值和标签绑定(bind)到 ng-model

javascript - react Apollo : Dynamically update GraphQL query from Component state