Ng-重复组| AngularJS

时间:2017-05-05 23:44:29

标签: javascript jquery angularjs

我有数据,类似这样(它来自SWARM,后端):

$scope.games = [
  {"categories":["1"],"id":"id1"},
  {"categories":["1"],"id":"id2"},
  {"categories":["1"],"id":"id3"},
  {"categories":["1"],"id":"id4"},
  {"categories":["2"],"id":"id5"},
  {"categories":["2"],"id":"id6"},
  {"categories":["2"],"id":"id7"},
  {"categories":["3"],"id":"id8"},
  {"categories":["3"],"id":"id9"}
  {"categories":["4"],"id":"id10"}
];

所以,现在它在内容中可见而没有任何类别分组,比如

id1 id2 id3 id4 id5 id6 id7 id8 id9 id10

但我需要这样做:

Category '1'
id1 id2 id3 id4

Category '2'
id5 id6 id7 

Category '3'
id8 id9 

Category '4'
id10

HTML来源:

<ul>
  <li ng-repeat="game in games by game.id">
     ...               
  </li>
</ul>

任何想法如何? 提前谢谢!

2 个答案:

答案 0 :(得分:0)

为什么不把日期结构化一点呢? 对于(最简单的)示例:

$scope.games = {
  "1": ["id1", "id2", "id3", "id4"],
  "2": ["id5", "id6", "id7"]
}

......等等。

答案 1 :(得分:0)

试试这个。

<ul>
    <li ng-repeat="(categories, catGames) in (games | groupBy: 
         'categories')">
    {{categories}}
    <ul>
        <li ng-repeat="game in catGames">
        {{game.id}}
        </li>
    </ul>
    </li>