简体   繁体   English

比例组的角度阵列和ng-repeat

[英]Angular array and ng-repeat for ratio group

I want to make a questionnaire which I can fill directly in the app's variables, due I don't have any database in this little project. 我想做一个问卷,可以直接填写应用程序的变量,因为在这个小项目中没有任何数据库。 The questionaire is made by 20 questions and I am using radio buttons for selecting the answer. 问卷由20个问题组成,我正在使用单选按钮选择答案。 At end end of the questionaire I will need to show the selected answers, for this I did the scope object testResult 在问卷的结尾,我将需要显示选择的答案,为此,我做了范围对象testResult

Initially, I was thinking to fill all 20 questions and radio variables manually, but I saw a ng-repeat angular function that can be helpful, and will like to know if I can use it in the this case. 最初,我想手动填写所有20个问题和无线电变量,但是我看到了ng-repeat角度函数,该函数可能会有所帮助,并且想知道在这种情况下是否可以使用它。

I may be storing the initial values of each radio group inside and array, and then print them in the view using ng-repeat. 我可能将每个无线电组的初始值存储在和数组中,然后使用ng-repeat在视图中打印它们。 I don't really have a clue where to start. 我真的不知道从哪里开始。

The next are piece of code related to my 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 I need to repeat is: 我需要重复的是:

<div class="radio">
    <label><input type="radio" name="qs01" ng-model="testResult.qs01" value="Question Variable">
        Question Variable
    </label>
</div>

You are putting different labels on each radio. 您在每个收音机上都贴有不同的标签。 But each radio is an option for the value of the same question . 但是每个收音机都是相同问题价值的选择。

You need to include radio options array to use ng-repeat. 您需要包括单选选项数组才能使用ng-repeat。 So you could 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 the options for 1 question . 这将重复1个问题的选项。 The name stays the same because that's how radio inputs for the same field work in a form. 名称保持不变,因为这是表单中同一字段的无线电输入的工作方式。

If you want to have multiple questions, each with multiple options, you need to use an array of arrays; 如果要有多个问题,每个问题有多个选项,则需要使用一个数组数组; each object would hold an array of options for that question. 每个对象都会包含该问题的一系列选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM