0

I am trying to develop a dynamic menu with angular and bootstrap for angular. Specifically, I have been able to get the menus and submenus to display on the page, but the submenus are drawn on top of each other.

To do this development I have started from the examples on the page "Angular for Bootstrap" https://ng-bootstrap.github.io/stackblitzes/dropdown/basic/stackblitz.html, adding * ngFor to make it dynamic but I have run into the problem of overlapping the buttons. Could someone guide me on how I could fix the problem?

<div class="btn-group mr-3" *ngFor="let appMenu of appMenus">
  <div class="btn-group" ngbDropdown role="group" aria-label="Button group with nested dropdown" >
    <button class="btn btn-outline-primary" ngbDropdownToggle>{{ appMenu.appMenuName }}</button>
    <div class="dropdown-menu" *ngFor="let appMenuItem of appMenu.appMenuItemList" ngbDropdownMenu>
      <button ngbDropdownItem [routerLink]="appMenuItem.path">{{ appMenuItem.appMenuItemName }}</button>
    </div>
  </div>
</div>
enter image description here

Thank you very much in advance.

2
  • The stackblitz link doesn't work. Is it public? What does the css look like? Commented Jan 14, 2021 at 21:57
  • 1
    I have corrected the link.The CSS is like the link example. Commented Jan 14, 2021 at 22:01

1 Answer 1

1

Solution is to put the *ngFor inside the button rather than the div:

<div class="btn-group mr-3" *ngFor="let appMenu of appMenus">
  <div class="btn-group" ngbDropdown role="group" aria-label="Button group with nested dropdown" >
    <button class="btn btn-outline-primary" ngbDropdownToggle>{{ appMenu.appMenuName }}</button>
    <div class="dropdown-menu" ngbDropdownMenu>
      <button *ngFor="let appMenuItem of appMenu.appMenuItemList" ngbDropdownItem [routerLink]="appMenuItem.path">{{ appMenuItem.appMenuItemName }}</button>
    </div>
  </div>
</div>

I think because you have the loop with ngbDropdownMenu in that div, it thinks you're trying to create a new menu for every iteration of the loop. So it's basically creating a ton of menus with one item and attaching them all to that one div.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.