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

时间:2018-07-28 17:06:44

标签: javascript arrays angularjs checkbox angularjs-ng-repeat

我正在尝试使用服务器端在angular js中构建测验应用程序,因为我有2种选择:

  1. 一个问题(input-checkbox)的多个选项。 "dataType": 3,
  2. 单个选项 一个问题。 (input-radio) "dataType": 2,

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

注意storedAnswer数组包含option.questionChoiceValue

我的问题是如何在选择多项(checkbox)问题时填充数组值:

这是我的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"
    }
]

1 个答案:

答案 0 :(得分:1)

我认为您应该在收音机中使用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);" />