简体   繁体   中英

Dynamic Menu with Sub menu in angular 2

I have some JSON in the below format. I need to convert it into a menu with a sub menu when sub-menu_location is not equal to null, as below 图像中的菜单栏

 { "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" } ] } 

A couple of things to note:

  • The example data you give in the question would only allow for a single sub-menu item.
  • I do not know how you want to display this but from the picture I am guessing Bootstrap.
  • I don't think Bootstrap supports dropdown sub-items by default so I am going to use the syntax in this codepen example

Potential solution

If you store the data object as a property in your component, the following template should give you something to start with:

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM