Angularjs - 嵌套数组

时间:2015-10-15 02:01:08

标签: json angularjs

我正在尝试设计

的数据模型
-> Section
   -> Sub Section
      ->Main Element
      ->Main Element
   -> Sub Section
      ->Main Element
      ->Main Element
      ->Main Element
...

这就是我现在所拥有的:

[{
  'section': 'market map',
  'subsections': ['aa','bb'],
  'mainelements': 
      [
        ['cc','dd'],
        ['ee','ff'],
      ]
}];

这就是我想要的:

-> Market Map
   -> aa
      ->cc
      ->dd
   -> bb
      ->ee
      ->ff

这是我的HTML:

<ul>
        <li ng-repeat="data in datas">
                <ul>
                <li ng-repeat="subsection in data.subsections">
                       <input type="text" ng-model="subsection"  size="30" placeholder="add subsection here">
                       <ul>
                        <li ng-repeat="mainelement in audit.mainelements">
                            <input type="text" ng-model="mainelement"  size="30" placeholder="add mainelement here">
                        </li>
                       </ul>
                </li>
                </ul>
         </li>
</ul>

我明白了:

-> Market Map
   -> aa
      ->[cc, dd]
   -> bb
      ->[ee, ff]
  1. 如何获得我想要的结果?
  2. 有没有更好的方法为这种层次结构设计json结构?

1 个答案:

答案 0 :(得分:2)

让你的json设计像这样

$scope.datas=[{
  'section': 'market map',
  'subsections': [{
      subSection: 'aa',
      mainelements: ['cc','dd']
   },{
     subSection: 'bb',
     mainelements: ['ee','ff']
   }]
}];

然后你的html将是

<ul>
        <li ng-repeat="data in datas=">
            {{data.section}}
                <ul>
                <li ng-repeat="subsection in data.subsections">
                       <input type="text" ng-model="subsection.subsection"  size="30" placeholder="add subsection here">
                       <ul>
                        <li ng-repeat="mainelement in subsection.mainelements">
                            <input type="text" ng-model="mainelement"  size="30" placeholder="add mainelement here">
                        </li>
                       </ul>
                </li>
                </ul>
         </li>
</ul>