0

I'm using 3 nested ng-repeat for read a json and show several questions and his answers. Until here it's working but now I'm trying to store the seleted answers and send it to the API. I don't know why the selected answers are not being stored.

This is my view:

<form>
    <div class="panel panel-default" ng-repeat="section in questionsTest">
        <div class="panel-heading">
            <h1>{{ section.name }}</h1>
            <h3 class="panel-title">{{ section.name }}. {{
                section.description }}</h3>
        </div>
        <div class="panel-body" ng-repeat="question in section.questions">
            {{ question.statement }}
            <div ng-repeat="answer in question.answers">
                <!--  <label class="radio-inline"> <input type="{{ answer.type }}" ng-model="question.value"
                    name="{{ answer.id }}" id="{{ answer.id }}" value="{{ answer.id }}">
                    {{ answer.valor }}
                </label>-->
                <label class="radio-inline"> <input type="{{ answer.type }}" ng-model="question.valor"
                    name="{{ question.id }}" id="{{ answer.id }}" value="{{ answer.id }}">
                    {{ answer.valor }}
                </label>
            </div>
        </div>
    </div>
</form>

And this is the controller:

$scope.question = {};
$scope.testing = function(){
    console.log($scope.question);
};

$scope.testing is a testing function to see on the console the value of $scope.question

3

1 Answer 1

1

You html setup is correct, you're just not reading it the selected values correctly. To get a selected answer for the first question, use the following:

$scope.json.section[0].questions[0].value

It's because when you're putting question.valor into ng-model - it's actually a n-th question inside n-th section. Those n indexes are defined by the number of items inside your original data structure $scope.json.

To get all values, you can iterate over your original object:

$scope.testing = function () {
    var answers = {sections: []};

    for (var i = 0; i < $scope.json.section.length; i++) {
        if (!answers.sections[i]) {
            answers.sections[i] = {answers: []};
        }

        for (var j = 0; j < $scope.json.section[i].questions.length; j++) {
            if (!answers.sections[i].answers) {
                answers.sections[i].answers = [];
            }

            answers.sections[i].answers[j] = $scope.json.section[i].questions[j].value
        }
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Yes but how can I get all the values? I still don't get it
@proktovief, did my answer help?
@proktovief, sorry, d9nt understand what you mean

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.