代码之家  ›  专栏  ›  技术社区  ›  pressharp

根据来自服务器的值选择默认单选按钮选项

  •  0
  • pressharp  · 技术社区  · 11 年前

    我正在使用angularjs编写一个测验引擎,能够成功地加载带有选项的问题,并且还实现了NEXT和BACK按钮,现在可以正常工作,但我希望当用户单击BACK或NEXT按钮时,如果问题得到回答,它会与之前的选择一起预先选择。

    每个问题都有4个选项,我在选项名称“IsSelected”上有一个属性,我使用它来确定用户选择的上一个选项。

    我希望这样,当用户在交叉检查答案时,默认情况下,之前选择的任何一个选项都应该被选中,这就是我目前所处的位置。我看过这个问题

    Need to Default select an Angular JS Radio Button 这在设置默认值方面解决了类似的问题,但并不完全是这样。

    HTML代码:

    <div class="col-lg-offset-2 col-lg-5" data-ng-repeat="item in items" style=" border:#000 1px solid;padding:10px 40px 40px 40px">
            <h3 style="color: #0000ff">Question <b style="color:red">{{item.QId}}</b> of <b style="color:green">{{item.QCount}}</b></h3>
            <div>
    
    
                <!--<div id="first">{{item.QId}}</div>-->
                <h2>{{item.QId}}. {{item.QText}} </h2>
            </div>
            <div data-ng-repeat="choice in item.AnswerCh">
    
                <input type="radio" name="choize" data-ng-model="choice.IsSelected" value="choice.IsSelected" /> {{choice.Text}}
    
            </div>
            <br />
            <div id="ref" style="display: none">{{item.QId}}</div>
            <div id="you">{{choice.choize.Text}}</div>
    

    JS代码:

            $scope.getNext = function () {    
                var quet = $('#ref').html();
                var answ = $('#you').html();
                cbtFact.getNext().update({q:answ,v:quet },
                function (data, status, headers, config){         
                    $scope.items = [];
                    
                    $scope.items = data;
                },
                function (data, status, headers, config) {
                });
            };
    
            $scope.getPrevious = function () {
                var quet = $('#ref').html();
                var answ = $('#you').html();
                cbtFact.getPrevious().update({ q: answ, v: quet },
                function (data, status, headers, config) {
                    $scope.items = [];
                    $scope.items = data;
                 
                function (data, status, headers, config) {
                });
    
            };
            
    1 回复  |  直到 11 年前
        1
  •  1
  •   ssilas777    11 年前

    使用 ng-if 这样地。。

    Fiddle

    <div data-ng-repeat="choice in item.AnswerCh">
       <span ng-if="choice.IsSelected">
          <input type="radio" name="choize" data-ng-model="choice.IsSelected" value="choice.IsSelected" checked/> {{choice.Text}}
       </span>
       <span ng-if="!choice.IsSelected">
          <input type="radio" name="choize" data-ng-model="choice.IsSelected" value="choice.IsSelected" /> {{choice.Text}}
       </span>
    </div>
    

    或使用Angular Js ngChecked 属性

    Fiddle

     <div data-ng-repeat="choice in item.AnswerCh">    
         <input type="radio" name="choize" data-ng-model="choice.IsSelected" value="choice.IsSelected" data-ng-checked="choice.IsSelected" /> {{choice.Text}}    
     </div>
    

    更新:

    使用 ng-model ng-value

    Fiddle

    尽管以上两种代码都有效,但在使用时仍存在一些问题 ng模型 .

    根据Angular Js documentation 这种功能可以使用ng模型和ng值本身实现,因此这应该是 推荐 方法 ng值 应为选定表达式时应设置的值。

    <div data-ng-repeat="choice in item.AnswerCh">
            <input type="radio" name="choize" data-ng-model="choice.IsSelected" ng-value="true" /> {{choice.Text}}       
     </div>