navbar not rendering in browser using ng-bootstrap & angular 4
As there is no navigation component you'd need to use a combination of the collapse functionality ng-bootstrap's ngbCollapse component as well as the dropdown functionality from the NgbDropdown component.
You'll need to bind an instance of ngbCollapse
to the div.collapse.navbar-collapse
and a boolean property on your component class. ng-bootstrap doesn't have any use for data-*
so you can remove attributes such as data-toggle="collapse"
.
You control the open/close state of the collapse menu via a property on your component class that gets toggled true
/false
through something like a click event. In this example is toggle via a (click)
event handler on the button.navbag-toggler
executing a method on the component toggleMenu()
which simply inverts the value of boolean property isCollapsed
via the !
operator.
For the menu item dropdown menu, you'd use the NgbDropdown component. You'd apply attributes ngbDropdown
and ngbDropdownToggle
to the container element and toggle element respectively.
<div class="nav-item dropdown" ngbDropdown>
<a class="nav-link dropdown-toggle" id="navbarDropdownMenuLink" ngbDropdownToggle>
Dropdown
</a>
<div class="dropdown-menu" aria-labelledby="dropdownBasic1">
<button class="dropdown-item">Action - 1</button>
<button class="dropdown-item">Another Action</button>
<button class="dropdown-item">Something else is here</button>
</div>
</div>
In terms of styling for Bootstrap 4 navbar, you'll need to use the following classes:
Navbars require a wrapping .navbar with .navbar-toggleable-* for responsive collapsing and color scheme classes.
Also elements such as button.navbar-toggle
are now button.navbar-toggler
with an "r" in Bootstrap 4. You use classes navbar-inverse
and bg-inverse
for the standard inverse navbar.
HTML:
<nav class="navbar navbar-toggleable-md navbar-inverse bg-inverse">
<button class="navbar-toggler navbar-toggler-right" type="button" aria-controls="appNavigation" [attr.aria-expanded]="!isCollapsed" aria-label="Toggle navigation" (click)="toggleMenu()">
<span class="navbar-toggler-icon"></span>
</button>
<a class="navbar-brand" href="#">My App</a>
<div class="collapse navbar-collapse" id="appNavigation" [ngbCollapse]="isCollapsed">
<div class="navbar-nav mr-auto">
<a class="nav-item nav-link" routerLink="" routerLinkActive="active">Home</a>
<a class="nav-item nav-link" routerLink="/about" routerLinkActive="active">About</a>
<div class="nav-item dropdown" ngbDropdown>
<a class="nav-link dropdown-toggle" id="navbarDropdownMenuLink" ngbDropdownToggle>
Dropdown
</a>
<div class="dropdown-menu" aria-labelledby="dropdownBasic1">
<button class="dropdown-item">Action - 1</button>
<button class="dropdown-item">Another Action</button>
<button class="dropdown-item">Something else is here</button>
</div>
</div>
</div>
</div>
</nav>
TS:
export class NavigationComponent {
isCollapsed = true;
constructor() {}
toggleMenu() {
this.isCollapsed = !this.isCollapsed;
}
}
Here is a plunker demonstrating the functionality and styling in action.