2016-11-27 104 views
0

我有很多ng-repeat單選按鈕的列表。 我已經成功獲取選定的項目。 現在我嘗試設置收音機的默認值。 有人可以幫助我。這裏是我的按鈕代碼。 感謝的rnested單選按鈕的設置值

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; 
 
}
<ion-list id="page-list7" ng-show="VariableRadioToggle2"> 
 
    <div ng-repeat="itemGrp in itemGroups"> 
 
     <ion-item class="item-divider" id="page-list-item-divider32">{{itemGrp.name}}</ion-item> 
 
     <ion-toggle name="{{itemGrp.name}}" ng-click="clickedVariableRadio2(portion.selected)" ng-repeat="(key,portion) in itemGrp.Portions" ng-model="itemGrp.selected_item" ng-true-value="'{{key}}'" ng-false-value="">{{portion.name}}</ion-toggle> 
 
    </div> 
 
</ion-list>

+0

'itemGrp.selected_item'應該是默認值。 – Erevald

+0

感謝您的快速回答。我怎樣才能做到這一點 ?我已經在ng-repeat div中嘗試了ng-init =「itemGrp.selected_item」。我已經嘗試過ng-init =「itemGrp.selected_item」/ ng-value =「itemGrp.selected_item」/ ng-checked =「itemGrp.selected_item」in ion-toggle div ...但是任何工作?在控制器部分還有其他的事嗎? – JEKES

+0

你可以修改'VariableRadioArr2'作爲子數組嗎? '[{「id」:「21」,「libelle」:「P15m」,「type」:「radio」,「values」:[{「value」:「name」,..}]} ..] ' – Erevald

回答

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>

+0

謝謝,它適用於我 – JEKES