1

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

  1. Multiple options for one question (input-checkbox). "dataType": 3,
  2. Single option for one question. (input-radio) "dataType": 2,

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

Note: storedAnswer array contains the option.questionChoiceValue's

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

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>

I have an object like this:

[
    {
        "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 Answer 1

1

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);" /> 
Sign up to request clarification or add additional context in comments.

3 Comments

radio is working fine i have a problem with checkboxes because storedAnswer is an array
So you're asking how to mark the radios as selected on initialization based on the storedAnswers array?
In order to work, the ng-change directive needs the ng-model directive and the ng-checked directive doesn' t play nice with the ng-model directive.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.