1

i am kind of stuck here, i am new to angular js.

I have a form having number of questions. These questions are loading with angularjs.

html for form is

<form ng-controller="controller" ng-submit="submitFirstQuiz()">
<div ng-repeat = "question in quiz.questions">
{{question}}
<label data-ng-repeat="choice in quiz.choices" style="float:left;">
    <input name="{{question}}" type="radio" value="{{choice}}"/>
</label>
<br/>
</div>
<input type="submit" value="submit"></input>
</ng-form>

and my js is

'use strict';

var QuizModule = angular.module('quiz');

QuizModule.controller('PersonalityQuizCtrl', function($scope){

    $scope.quiz = {
            questions: ["q1","q2","q3"],
            choices: ["5","4","3","2","1"]
        };

    $scope.submitFirstQuiz = function(){
        console.debug("how to print selected choices");
    };

});

The above code is generating my form as i want. Now how can i get the selected values of the radio buttons, in other word how can i submit the form with selected value. I don't how to bind the result data. Kindly help me, i shall be thankful. Regards,

1 Answer 1

1

HTML:

<input name="{{question}}" ng-click="radioSelected(choice, question)" 
type="radio" value="{{choice}}"/>

JS:

'use strict';

var QuizModule = angular.module('quiz');

QuizModule.controller('PersonalityQuizCtrl', function($scope){

    $scope.quiz = {
            questions: ["q1","q2","q3"],
            choices: ["5","4","3","2","1"]
        };

    $scope.selectedChoices = {};

    $scope.radioSelected = function(choice, question){        
        $scope.selectedChoices[question] = choice;
    }
    $scope.submitFirstQuiz = function(){
        console.log($scope.selectedChoices);
    };

});
Sign up to request clarification or add additional context in comments.

1 Comment

now all respective buttons for all questions are getting selected, i have added the screen shot. for example. if i select first choice for question 1, choice one is automatically selected for all questions.

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.