[英]display list from complex json using angular
I have this JSON (I did not create it and can not change it) 我有这个JSON(我没有创建它,也无法更改它)
{"tags": [
{
"title": "news",
"options": [
{
"title": "important",
},
{
"title": "less important",
}
]
},
{
"title": "entertainment",
"options": [
{
"title": "entertainment",
}
]
}
I would like to display it as a list, where if a certain title hasonly one option then the title should be displayed in the list, and if a title has more than one item in options, then the main title should be displayed and the options titles should also be displayed. 我想将其显示为列表,如果某个标题只有一个选项,那么该标题应显示在列表中;如果某个标题中有多个选项,则应显示主标题和选项标题也应显示。 the final result (according to the above example) should display:
最终结果(根据上述示例)应显示:
<ul>
<li>news<ul>
<li>important</li>
<li>less important</li>
</ul>
</li>
<li>entertainment</li>
</ul>
how can I do this in angularJS? 我该如何在angularJS中做到这一点?
You can try with something like that if myList
is the name that you use in the $scope
for the json object. 如果
myList
是在json对象的$scope
使用的名称,则可以尝试使用类似的方法。 Basically is an ng-repeat
with an inner ng-repeat
基本上是一个
ng-repeat
具有内ng-repeat
<ul ng-repeat="element in myList.tags">
<li>
{{element.title}}
<ul ng-repeat="innerElement in element.options">
<li>{{innerElement.title}}</li>
</ul>
</li>
</ul>
I am assuming your json file is stored in a variable called result 我假设您的json文件存储在一个名为result的变量中
<ul ng-repeat = "tg in result.tags">
<li ng-if="tg.options.length > 1">{{tg.title}}
<ul ng-repeat ="innerElement in tg.options" >
<li>{{innerElement.title}}</li>
</ul>
</li>
<li ng-if="tg.options.length==1">{{tg.title}}</li>
</ul>
Thnx, both above suggestions helped clear out the solution. Thnx,以上两个建议均有助于解决方案。 I needed to nest two
ng-repeat
loops together and apply an ng-if
to the inner loop. 我需要将两个
ng-repeat
循环嵌套在一起,并将ng-if
应用于内部循环。
I ended up with this soultion: 我最终得到了这种灵魂:
<ul> <li ng-repeat="x in myData.tags"> {{x.title}} <ul ng-repeat="y in x.options" ng-if="x.options.length>1"> {{y.title}} </ul> </li> </ul>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.