2016-05-23 47 views
0

我有一個JSON對象嵌套,我需要基於什麼從JSON文件,意義進來(動態)來填充複選框,我不知道事先提供這些字段的名稱,或者可能有多少元素。這是一個樣本,但可能會有更多的孩子,每個孩子可能有更多或更少的項目。角JS - 填充複選框,並跟蹤它基於JSON文件

{ 
"Parent":{ 

"Child" : 

     { 
      "Child_01" : { 

       "description_01":false, 
       "description_02":false, 
       "description_03":false, 
       "description_04":false, 
       "description_05":false 
      }, 


      "Child_02" : 
       { 
       "something_01":false, 
       "something_02":false, 
       "something_03":false, 
       "something_04": false 
       }, 

       "Child_03" : 
       { 
       "things_01":false, 
       "things_02":false, 
       "things_03":false, 
       "things_04":false, 
       "things_05":false 
       } 

     } 

} 
} 

現在,我需要填充性降低下來,(child_01,child_02,child_03 ....),當用戶選擇其中之一,我需要顯示其所有屬性的複選框。當然可以跟蹤所選的一個。

這是我的。

$scope.children = response.data.parent.child; 
$scope.childrenList = []; 

angular.forEach($scope.children, function(value1, key1){ 
    $scope.childrenList.push(key1); 

}); 

和HTML:

<div ng-repeat="(key, data) in children"> 
       <input ng-model="childrenListModel" type="checkbox" name="{{child}}" value="{{child}}">{{child}}</input> 

</div> 

沒有什麼工作,我打算的方式。任何幫助,將不勝感激。

回答

0

你可能需要幾個嵌套ng-repeat秒。看看這個工作的重擊者:http://plnkr.co/edit/K92JI7UlW9h6gh8SPeU5?p=preview

<div ng-repeat="child in children.Parent.Child"> 

    <div ng-repeat="options in child"> 

    <div ng-repeat="option in options"> 

     <div ng-repeat="(key, val) in option"> 

     <label for="{{key}}"> 
      {{key}} 
      <input name="{{key}}" type="checkbox" ng-model="option[key]" /> 
     </label> 


     </div> 

    </div> 
    <hr /> 

    </div> 

</div> 
+0

這是偉大的,我明白了。我怎樣纔能有一個下拉,所以我只看到選擇的孩子的屬性? – Ahoo

+0

有兩種方法:(1)你在'getChild()'中做'child',而不是'child.parent.Child'中的child。 getChild()將需要返回所選子項的數組。 (2)在兒童下拉菜單中,使用'ng-model =「selectedChild」',然後在選項下拉菜單中,執行selectedChild中的'ng-options ='選項。 – Kyle

+0

問問你是否可以在重擊者中做一個選項?在一個用getChild() – Ahoo