javascript - 如何使用 AngularJS 填充复选框值列表?

标签 javascript arrays angularjs checkbox angularjs-ng-repeat

我正在尝试在服务器端使用 Angular JS 构建一个测验应用程序,因为我有两种类型的选择:

  1. 一个问题有多个选项(输入复选框)“数据类型”:3,
  2. 单一选项 对于一个问题。 (输入 radio ) “dataType”:2,

我正在尝试填充多个checkbox问题的存储答案,在这种情况下storedAnswer是一个数组,而storedAnswer是一个整数值输入 radio

注意: storedAnswer 数组包含 option.questionChoiceValue

我的问题是如何填充数组值作为多选(复选框)问题的检查:

这是我的 HTML 代码:

<div ng-repeat="question in data.questionData">

    <span name="multiSelectCheckBox" ng-if="question.dataType == 3">
        <div ng-repeat="option in question.choices " class="">
            <label for="mul_{{option.questionChoiceValue}}_ques{{option.questionId}}">
                <span ng-repeat="stAns in question.storedAnswer"
                      ng-if="stAns == option.questionChoiceValue">
                    <input id="mul_{{option.questionChoiceValue}}_ques{{option.questionId}}" 
                        type="checkbox"
                        ng-model="question.storedAnswer"
                        ng-change="onSelect(question, option);" /> 
                      {{option.questionChoiceText}}
                </span>
            </label>
        </div>
    </span>

    <span name="multiSelectRadio" ng-if="question.dataType == 2">
        <div ng-repeat="option in question.choices  " class="">
            <label for="sin_{{option.questionChoiceValue}}_ques{{option.questionId}}">

                <input type="radio" id="sin_{{option.questionChoiceValue}}_ques{{option.questionId}}" 
                    ng-model="question.storedAnswer" 
                    value="{{option.questionChoiceValue | number}}"
                    name="que_ques{{option.questionId}}"
                    ng-change="onSelect(question, option);" />
                  {{option.questionChoiceText}}                 
            </label>
        </div>
    </span>
</div>

我有一个像这样的对象:

[
    {
        "marked": "false",
        "questionId": "7d00a35ddb6313004f3bdde748961969",
        "helpText": "",
        "answered": "true",
        "storedAnswer": [
            1,
            5
        ],
        "questionSno": 1,
        "dataType": 3,
        "choices": [
            {
                "questionId": "7d00a35ddb6313004f3bdde748961969",
                "questionChoiceValue": 5,
                "questionChoiceText": "Float, long double"
            },
            {
                "questionId": "7d00a35ddb6313004f3bdde748961969",
                "questionChoiceValue": 4,
                "questionChoiceText": "Float"
            },
            {
                "questionId": "7d00a35ddb6313004f3bdde748961969",
                "questionChoiceValue": 3,
                "questionChoiceText": "float, double, long double"
            },
            {
                "questionId": "7d00a35ddb6313004f3bdde748961969",
                "questionChoiceValue": 2,
                "questionChoiceText": "long double, short int"
            },
            {
                "questionId": "7d00a35ddb6313004f3bdde748961969",
                "questionChoiceValue": 1,
                "questionChoiceText": "short int, double, long int, float"
            }
        ],
        "questionText": "In C, what are the various types of real data type (floating point data type)?",
        "selected": "false"
    },
    {
        "marked": "false",
        "questionId": "93e5c5e5db6713004f3bdde748961957",
        "helpText": "",
        "answered": "false",
        "storedAnswer": 1,
        "questionSno": 3,
        "dataType": 2,
        "choices": [
            {
                "questionId": "93e5c5e5db6713004f3bdde748961957",
                "questionChoiceValue": 3,
                "questionChoiceText": "May be"
            },
            {
                "questionId": "93e5c5e5db6713004f3bdde748961957",
                "questionChoiceValue": 2,
                "questionChoiceText": "False"
            },
            {
                "questionId": "93e5c5e5db6713004f3bdde748961957",
                "questionChoiceValue": 1,
                "questionChoiceText": "True"
            }
        ],
        "questionText": "A macro can execute faster than a function.",
        "selected": "false"
    }
]

最佳答案

我相信您的 radio 应该使用 ng-checked 而不是 ng-model。像这样:

<input type="radio" id="sin_{{option.questionChoiceValue}}_ques{{option.questionId}}"
       ng-checked="question.storedAnswer"
       value="{{option.questionChoiceValue | number}}"
       name="que_ques{{option.questionId}}"
       ng-change="onSelect(question, option);" /> 

关于javascript - 如何使用 AngularJS 填充复选框值列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51573515/

相关文章:

javascript - 为什么在访问匿名自执行函数的参数时需要使用 "this"关键字?

javascript - While循环: Generated table rows and column inconsistency

arrays - Kotlin - 向空数组添加值

javascript - Visual Studio 2015 中的 AngularJS 智能感知

javascript - 普通 JavaScript 到 AngularJS

javascript - 重试后如何获取catchWhen?

javascript - Rails 远程 JSON 调用阻塞 Controller 线程

c++ - 如何为我正在编写的《神之浩劫》迷你游戏创建和访问对象数组

python - 对具有 NaN 值的 Numpy 数组进行反向排序

javascript - 如何从 ngModel 渲染 HTML 标签?