当来自循环时,如何显示自动选择到单选按钮?

时间:2019-04-11 11:20:04

标签: angularjs loops radio-button selected

我有一个尝试在线考试的小应用程序。我的问题集来自数据库使用循环。我想编辑我已经尝试过的考试。我有一系列问题的答案。但是我无法显示自动选择以尝试单选按钮。

这是我的答案

{“ 1”:“ A”,“ 2”:“ A”,“ 6”:“ A”,“ 10”:“ A”,“ 14”:“ A”,“ 21”:“ B “,” 26“:” C“,” 31“:” B“,” 33“:” C“,” 34“:” B“,” 54“:” C“}

这是我使用循环显示在查看页面上的问题

<div class="row" style="margin-top: 20px;">
<section ng-repeat="count in subject.test_count" ng-cloak  class="col col-3">
    <div class="inline-group" ng-if="count%2!='0'">
        <b style="width: 25px;display: inline-grid;">{{count}}</b>
        <input type="radio" name="{{subject.name}}{{count}}"
               id="a{{subject.name}}{{count}}" class="input-hidden"
               ng-model="questiondata[subject.name][count]"
               value="A" ng-checked="true">
            <label for="a{{subject.name}}{{count}}" >
            <p>A</p>
            </label>

        <input type="radio" name="{{subject.name}}{{count}}" 
               id="b{{subject.name}}{{count}}" class="input-hidden"
               ng-model="questiondata[subject.name][count]" value="B">
        <label for="b{{subject.name}}{{count}}" >
            <p>B</p>
        </label>

        <input type="radio" name="{{subject.name}}{{count}}"
               id="c{{subject.name}}{{count}}" class="input-hidden"
               ng-model="questiondata[subject.name][count]" value="C">  
        <label for="c{{subject.name}}{{count}}" >
            <p>C</p>
        </label>

        <input type="radio" name="{{subject.name}}{{count}}"
               id="d{{subject.name}}{{count}}" class="input-hidden"
               ng-model="questiondata[subject.name][count]" value="D">
        <label for="d{{subject.name}}{{count}}" > 
            <p>D</p>
        </label>

        <input type="radio" name="{{subject.name}}{{count}}"
               id="e{{subject.name}}{{count}}" class="input-hidden"
               ng-model="questiondata[subject.name][count]" value="E">
        <label for="e{{subject.name}}{{count}}" >
            <p>E</p>
        </label>

    </div>
</section>

1 个答案:

答案 0 :(得分:0)

与此有关的几件事。首先是您要对test_count的值而不是实际对象进行ngRepeing,以解决您将要执行以下操作:

    <section ng-repeat="(key,value) in subject.test_count" ng-cloak  class="col col-3">
    {{key}} {{value}} 
    <span class="inline-group" ng-if="count%2!='0'">
        <input type="radio" name="{{subject.name}}{{key}}"
               id="a{{subject.name}}{{key}}" class="input-hidden"
               ng-model="questiondata[subject.name][key]"
               value="A">
            <label for="a{{subject.name}}{{key}}" >A</label>

        <input type="radio" name="{{subject.name}}{{key}}" 
               id="b{{subject.name}}{{key}}" class="input-hidden"
               ng-model="questiondata[subject.name][key]" value="B">
        <label for="b{{subject.name}}{{key}}" >B</label>

        <input type="radio" name="{{subject.name}}{{key}}"
               id="c{{subject.name}}{{key}}" class="input-hidden"
               ng-model="questiondata[subject.name][key]" value="C">  
        <label for="c{{subject.name}}{{key}}" >C</label>

        <input type="radio" name="{{subject.name}}{{key}}"
               id="d{{subject.name}}{{key}}" class="input-hidden"
               ng-model="questiondata[subject.name][key]" value="D">
        <label for="d{{subject.name}}{{key}}" > D</label>

        <input type="radio" name="{{subject.name}}{{key}}"
               id="e{{subject.name}}{{key}}" class="input-hidden"
               ng-model="questiondata[subject.name][key]" value="E">
        <label for="e{{subject.name}}{{key}}" >E</label>

    </span>
</section>

一旦您有了单选按钮并进行了主题匹配(我对您认为数据的设置方式有所保留),就需要使用上面提供的值来播种问题数据:

$scope.questiondata[0] = angular.copy($scope.subject.test_count);

现在我们可以看到$ scope.questiondata [0] [1] = $ scope.subject.test_count [1] ='A',这意味着将选择A。

以下是完整控制器的示例:https://plnkr.co/edit/fyJwfLc6Qb7b7p4ZLgqI