设置rnested单选按钮的值

时间:2016-11-27 22:43:37

标签: javascript angularjs ionic-framework

我在ng-repeat中有很多单选按钮。 我成功获得了所选项目。 现在我尝试设置收音机的默认值。 有谁可以帮助我吗。这是我的按钮代码。 感谢' S



/usr/bin/python  *.py

VariableRadioArr2 = [{"id":"21","libelle":"P15m","type":"radio","valeur_1":"Oui","valeur_2":"Non","valeur_3":"","valeur_4":"","valeur_5":"","valeur_6":"","valeur_7":"","valeur_8":"","valeur_9":"","valeur_10":""},{"id":"25","libelle":"Surface (m²)","type":"input","valeur_1":"","valeur_2":"","valeur_3":"","valeur_4":"","valeur_5":"","valeur_6":"","valeur_7":"","valeur_8":"","valeur_9":"","valeur_10":""},{"id":"36","libelle":"p16","type":"radio","valeur_1":"Oui","valeur_2":"Non","valeur_3":"","valeur_4":"","valeur_5":"","valeur_6":"","valeur_7":"","valeur_8":"","valeur_9":"","valeur_10":""}]

$scope.itemGroups = VariableRadioArr2;
console.log(VariableRadioArr2);
//$scope.selected = 1;
$scope.VariableRadioToggle2 = true;

$scope.clickedVariableRadio2 = function(test)
{
    $scope.clickedVariableRadio2_select = JSON.parse(JSON.stringify($scope.itemGroups));
}

if(VariableRadioArr2.length == 0) 
{
    $scope.clickedVariableRadio2_select = [];
    $scope.VariableRadioToggle2 = false;
}




1 个答案:

答案 0 :(得分:0)

这里我从1-10开始循环,将值复制到新对象中并删除旧对象。

var myApp = angular.module('myApp',[]);
function MyCtrl($scope) {
    var Variable = [{"id":"21","libelle":"P15m","type":"radio","valeur_1":"Oui","valeur_2":"Non","valeur_3":"","valeur_4":"","valeur_5":"","valeur_6":"","valeur_7":"","valeur_8":"","valeur_9":"","valeur_10":""},{"id":"25","libelle":"Surface (m²)","type":"input","valeur_1":"","valeur_2":"","valeur_3":"","valeur_4":"","valeur_5":"","valeur_6":"","valeur_7":"","valeur_8":"","valeur_9":"","valeur_10":""},{"id":"36","libelle":"p16","type":"radio","valeur_1":"Oui","valeur_2":"Non","valeur_3":"","valeur_4":"","valeur_5":"","valeur_6":"","valeur_7":"","valeur_8":"","valeur_9":"","valeur_10":""}];

  // Create sub object for all values 
  // and delete the old values
  for (i = 0; i < Variable.length; i++) {
      Variable[i].values = {};
      Variable[i].selected = '';

      for (v = 1; v < 11; v++) {
        if(Variable[i]['valeur_'+v] != undefined){
          Variable[i].values['valeur_'+v] = Variable[i]['valeur_'+v];
          delete Variable[i]['valeur_'+v];
        }
      }
  }
  
  $scope.itemGroups = Variable;
  
  // Set defaults
  // By index
  $scope.itemGroups[1].selected = 'valeur_4';

}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js"></script>

<div ng-app="myApp" ng-controller="MyCtrl">
  <div ng-repeat="itemGrp in itemGroups">
    <div>{{itemGrp.libelle}}</div>
    <label ng-repeat="(key, value) in itemGrp.values">
      <input type="radio" name="{{itemGrp.id}}" ng-model="itemGrp.selected" ng-value="key">{{value}}
    </label>
  </div>
</div>