如何使用AngularJS填充复选框值列表? [英] How to populate list of checkbox values with AngularJS?

查看:92
本文介绍了如何使用AngularJS填充复选框值列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to build a quiz application in angular js with serverside for that i have 2 type of choices:

  1. 一个问题(input-checkbox)的多个选项. "dataType": 3,
  2. 单个选项 一个问题. (input-radio) "dataType": 2,
  1. Multiple options for one question (input-checkbox). "dataType": 3,
  2. Single option for one question. (input-radio) "dataType": 2,

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

I am trying to populate stored answer for multiple checkbox questions in this case storedAnswer is an array, whereas storedAnswer is an integer value for input radio

注意:storedAnswer数组包含option.questionChoiceValue

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

My question is how can i populate array value as checked for multiple choice (checkbox) question:

这是我的HTML代码:

This is my HTML code:

<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"
    }
]

推荐答案

我认为您应该在收音机中使用ng-checked而不是ng-model.像这样:

I believe you should be using ng-checked instead of ng-model for your radio. Like this:

<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);" /> 

这篇关于如何使用AngularJS填充复选框值列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆