我有一些像这样的json,它是从$http中提取出来的,并分配给作用域使用ng-repeat进行迭代。
杰森:
[{"id":19,"Name":"Apple Pie","Type":"Pies"},
{"id":20,"Name":"Old Fashioned Pumpkin Pie","Type":"Pies"},
{"id":21,"Name":"White Rolls","Type":"Rolls"},
{"id":27,"Name":"Honey Sandwich","Type":"Sandwich"}]我想要的是由ng-repeat制作的列表,类似于这个标记
-Pies
-苹果派
-老式南瓜派
-Rolls
-白卷
-Sandwich
-蜂蜜三明治
所以,而不是这个
<div class="form-group" ng-repeat="bread in breads">
<label for="exampleInputPassword1">@{{bread.Name}}</label>
</div>就像这样:
<div class="form-group" ng-repeat="bread in breads">
<label for="thing">@{{bread.Type}}</label><!--But only if first of type-->
<label for="exampleInputPassword1">@{{bread.Name}}</label>
</div>基本上,我唯一能想到的就是:
-Pies
-苹果派
-Pies
-老式南瓜派
当我不想因为有两个馅饼而重复两次“馅饼”时,我想按类型分开。$first属性只适用于我认为的整个集合,因此在这里没有帮助。除了改革JSON之外,我还有什么可以做的吗?
发布于 2016-10-28 04:09:40
你能做到的
<div ng-controller="MyCtrl">
<ul ng-repeat="(key, value) in breads | groupBy: 'Type'">
<b>{{ key }}</b>
<li ng-repeat="breado in value">
<i>{{ breado.Name }} </i>
</li>
</ul>
</div>演示
https://stackoverflow.com/questions/40297850
复制相似问题