我有以下格式的JSON。当sub-menu_location
不等于null时,我需要将其转换为带有子菜单的菜单,如下
{
"data": [
{
"menu_name": "Primary Operations",
"enabled": true,
"sub-menu_location": null
},
{
"menu_name": "Curated Games",
"enabled": false,
"sub-menu_location": null
},
{
"menu_name": "Cricket",
"enabled": false,
"sub-menu_location": "outdoor"
},
{
"menu_name": "football",
"enabled": false,
"sub-menu_location": "outdoor"
},
{
"menu_name": "Hockey",
"enabled": false,
"sub-menu_location": "outdoor"
}
]
}
答案 0 :(得分:0)
有几点需要注意:
如果将data
对象存储为组件中的属性,则以下模板应该为您提供一些内容:
<li>
<a class="dropdown-toggle" data-toggle="dropdown">Menu<b class="caret"></b></a>
<ul class="dropdown-menu multi-level">
<li *ngFor="let menuItem of data" class="dropdown-submenu">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">{{menuItem.menu_name}}</a>
<ul *ngIf="menuItem.sub-menu_location" class="dropdown-menu">
<li><a href="#">{{menuItem.sub-menu_location}}</a></li>
</ul>
</li>
</ul>
</li>