javascript - Angular array and ng-repeat for ratio group -
i want make questionnaire can fill directly in app's variables, due don't have database in little project. questionaire made 20 questions , using radio buttons selecting answer. @ end end of questionaire need show selected answers, did scope object testresult
initially, thinking fill 20 questions , radio variables manually, saw ng-repeat angular function can helpful, , know if can use in case.
i may storing initial values of each radio group inside , array, , print them in view using ng-repeat. don't have clue start.
the next piece of code related project.
view
<div class="form-group"> <div class="radio"> <label><input type="radio" name="qs01" ng-model="testresult.qs01" value="1"> question one. </label> </div> <div class="radio"> <label><input type="radio" name="qs01" ng-model="testresult.qs01" value="2"> question two. </label> </div> <div class="radio"> <label><input type="radio" name="qs01" ng-model="testresult.qs01" value="3"> question three. </label> </div> <div class="radio"> <label><input type="radio" name="qs01" ng-model="testresult.qs01" value="4"> question four. </label> </div> <div class="radio"> <label><input type="radio" name="qs01" ng-model="testresult.qs01" value="5"> question five. </label> </div> </div> app
// test result object $scope.testresult = {}; what need repeat is:
<div class="radio"> <label><input type="radio" name="qs01" ng-model="testresult.qs01" value="question variable"> question variable </label> </div>
you putting different labels on each radio. each radio option value of same question.
you need include radio options array use ng-repeat. do:
$scope.qs01options = ['blue', 'red', 'yellow'];
<div class="form-group"> <div class="radio"> <label>question one.</label> <input ng-repeat="opt in qs01options" type="radio" name="qs01" ng-model="testresult.qs01" value="{{opt}}"> </div> </div> this repeats options 1 question. name stays same because that's how radio inputs same field work in form.
if want have multiple questions, each multiple options, need use array of arrays; each object hold array of options question.
Comments
Post a Comment