单选按钮值无法正确呈现

时间:2017-03-27 11:06:53

标签: html angularjs json radio-button

我的单选按钮有问题。我从JSON获取所有值,如标签,组件等,并在视图上呈现它。

示例:我从JSON获得了组件单选按钮的两个值,我在视图中呈现它,但问题是当我选择第一个单选按钮时,其他无线电值未被选中。 注意:这两个都在不同的div。

  {
    "autoselect": [
      "nihil est velit"
    ], 
    "component": "radio", 
    "description": "distinctio accusamus cum quod veniam voluptatibus dolor", 
    "editable": false, 
    "label": "qui excepturi laborum", 
    "options": [
      "nihil est velit", 
      "ad aliquid id", 
      "irure", 
      "veniam voluptas", 
      "velit ea consectetur"
    ], 
    "required": true
  }, 
  {
    "autoselect": [
      "qui soluta"
    ], 
    "component": "radio", 
    "description": "est rerum dolor quisquam", 
    "editable": false, 
    "label": "deserunt quia", 
    "options": [
      "qui soluta", 
      "aperiam ut", 
      "et nostrud"
    ], 
    "required": true
  }, 
  {
    "autoselect": [
      "occaecati impedit proident"
    ], 
    "component": "radio", 
    "description": "id cumque totam sapiente reprehenderit", 
    "editable": false, 
    "label": "sit ut", 
    "options": [
      "corrupti", 
      "quidem", 
      "occaecati impedit proident"
    ], 
    "required": true
  }

HTML代码

<form name="myForm" role="form" novalidate class="form-horizontal" ng-hide="formMain">
      <div class="mainBody" ng-repeat="tag in renderTags.form_fields track by $index">
        <ng-form name="tag.form_name" id="tag.form_id">
          <div ng-switch on="tag.component" class="row">
            <div ng-switch-when="radio" class="form-group">
              <label class="control-label col-xs-3"><b>{{tag.label}}:</b></label>
              <div class="col-xs-2">
                <div class="form-control" ng-repeat="option in tag.options" ng-disabled="!tag.editable" ng-required="tag.required" style="margin-right:10px !important;">
                  <span ng-if="tag.autoselect!==null">
                                            <span ng-if="tag.autoselect[0] === option">
                                                <input type="radio" checked ng-value="option" name="$index" ng-click="clicked(option,tag)">{{option}}
                                            </span>
                  <span ng-if="tag.autoselect[0] !== option">
                                                <input name="$index" type="radio" ng-value="option">{{option}}
                                            </span>
                  </span>
                </div>
              </div>
            </div>
            <br/>
            <div ng-switch-default>

            </div>
          </div>
        </ng-form>
      </div>
    </form>

JS代码

var app = angular.module('myApp', []);

app.controller('MainCtrl', function($scope, $http) {
  $scope.getForm = function() {
    var formdata = $http({
        method: 'GET',
        url: 'data.json'
      })
      .then(function(response) {
        $scope.renderTags = response.data.data;
      }, function(reason) {
      });
  };

  $scope.clicked = function(option, tag) {
    var index = tag.autoselect.indexOf(option);
    if (index > -1) {
      tag.autoselect.splice(index, 1);
    } else {
      tag.autoselect.push(option);

    }
  }
});
你可以帮我解决这个问题吗? Link to Plunkr

尝试选中其中一个复选框,其他值将关闭。 提前致谢

1 个答案:

答案 0 :(得分:0)

name属性用于对单选按钮进行分组。将name属性设置为{{tag.label}},以便将一个标签下的所有单选按钮分组

同时使用带有ng-checked表达式的ng-checked="tag.autoselect[0] === option"来检查默认值

更改HTML <span>

<span ng-if="tag.autoselect!==null">
  <span ng-if="tag.autoselect[0] === option">
      <input type="radio" checked ng-value="option" name="$index" ng-click="clicked(option,tag)">{{option}}
   </span>
   <span ng-if="tag.autoselect[0] !== option">
      <input name="$index" type="radio" ng-value="option">{{option}}
   </span>
</span>

<span ng-if="tag.autoselect!==null">
  <span>
    <input type="radio" ng-checked="tag.autoselect[0] === option" name="{{tag.label}}" ng-click="clicked(option,tag)" />{{option}}
  </span>
</span>

以下是 UPDATED PLUNKER