I am developing a quiz application in angularJS. The HTML code is
`
<div class='question' ng-repeat='question in quiz ' value='{{$index}}'>
<h3>{{$index+1}}. {{question.q}}</h3>
<div ng-repeat = "options in question.options">
<input type='radio'> <strong>{{options.value}}</strong>
</div>
</div>
<input type="button" value="Submit" ng-click= submitAnswer()>
<script src="quizController.js"></script>
</body>
`
And the javascript file is
$scope.submitAnswer =function (){
}
I want when the user answers all the questions, all the values of radio button(answers) should be passed to submitAnswer() on clicking submit button.
Now every answer to each question will be stored in an array in the model. The structure of the model looks like this:
$scope.model.question[$index] = 'value'
Questions are indexed from 0, so first question is at
$scope.model.question[0]
. Now if you want to send it to the API, just send thequestion
array via$http
.First you need to put your input fields and button inside a form tag. Then add the ng-submit directive to the form and assign to it the submitAnswer() function.
Make sure the type of your button is "submit" as well, not "button".