Skip to content

Instantly share code, notes, and snippets.

@salmanx
Last active January 2, 2018 06:03
Show Gist options
  • Save salmanx/39d67a0f6abbc18dadcb8546c823046c to your computer and use it in GitHub Desktop.
Save salmanx/39d67a0f6abbc18dadcb8546c823046c to your computer and use it in GitHub Desktop.
simple angular directive for dropdown menu in navbar
import { Directive, HostBinding, HostListener } from '@angular/core';
@Directive({
selector: '[rbDropdown]'
})
export class DropdownDirective {
private isOpen = false;
@HostBinding('class.open') get opened(){
return this.isOpen;
}
@HostListener('click') open(){
this.isOpen = true;
}
@HostListener('mouseleave') close(){
this.isOpen = false;
}
}
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="/">Site Name</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li class="active"><a href="#">Link <span class="sr-only">(current)</span></a></li>
<li><a href="#">Link</a></li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li class="dropdown" rbDropdown> <!-- Add dropdown directive in dropdown class -->
<a href="#" class="dropdown-toggle">User <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="#">Login</a></li>
<li><a href="#">Another action</a></li>
<li role="separator" class="divider"></li>
<li><a href="#">Logout</a></li>
</ul>
</li>
</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container -->
</nav>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment