简体   繁体   中英

ng2-dropdown menu not working in angular2

I've been stuck on this for hours and hours and became pretty desperate. So I have this angular2 applicaton made for a school project but what I want to do is when i click on the + button this dropdown menu comes down and when i click on it again, or when I click on a link, it closes again. Right now it just stays open all the time and doesn't do anything when I click it

I have tried many different things, from ng2-dropdown to a javascript file that shows or hides the menu. I am out of options and getting really frustrated on this.

this is the html element in the browser before i click the +

this is the html element in the browser after i click the +

I have done the proper imports for the ng2-dropdown module which should make it work. It might be something small and stupid, but I don't see it.

This is my app.component which has the navigation bar as a template. on click i route to other components html's.

@Component({
selector: 'ls-app',
styleUrls: ['./app/css/fonts.css','./app/css/sass.css'],

template: `
<header id="header">
    <nav role='navigation' class="main-nav" id="main-nav">
        <ul id="main-nav-list">
            <div class="navgroup">
                <li><a [routerLink]="['/home']"class="navlogo" href="./leagues/home.component.html"><img class="logo" src="./app/img/logo.png" alt="logo"></a></li>
            </div>
            <div class="navgroup">
               <li>
<a [routerLink]="['/play']"class="navtitle" href="./leagues/play.component.html"><img class="imgMainNav" src="./app/img/play.svg" alt="play">Speel</a>
</li>
<li><a [routerLink]="['/stats']"class="navtitle"     href="./leagues/stats.component.html"><img class="imgMainNav"     src="./app/img/chart.png" alt="chart">Stats</a></li>
<li><a [routerLink]="['/leagues']"class="navtitle" href="./leagues/join-league.component.html"><img class="imgMainNav" src="./app/img/chart.png" alt="chart">Join League</a></li>

            </div>
            <div class="navgroup login">

                <div class="add-button" dropdown [dropdownToggle]="true">
                    <div  dropdown-open > +</div>

                    <ul class="dropdown" >

                        <a [routerLink]="['/account']"href="leagues/account.component.html"><li><i class="fa fa-check-square-o fa-2"></i>Mijn account</li></a>
                        <a [routerLink]="['/play']"href="leagues/play.component.html"><li><i class="fa fa-comments fa-2"></i>Plaats pronostiek</li></a>
                        <a [routerLink]="['/leagues']"href="leagues/join-league.component.html"><li><i class="fa fa-clipboard fa-2"></i>Nieuwe competitie</li></a>
                        <a href=""><li><i class="fa fa-user-plus fa-2"></i>Shop</li></a>
                        <a href=""><li><i class="fa fa-user-plus fa-2"></i>Log out</li></a>

                    </ul>
                </div>
                <li><a class="navtitle loginnav login-window" onclick="hierkomtdefunctievanjavascriptfile()" ><img class="imgMainNav" src="./app/img/fa-user.png" alt="login">Login/Register</a></li>
            </div>
        </ul>
    </nav>
</header>

And this is my css (actually sass but gets converted when run) for the dropdown class just in case. So when i click on the button the class is .dropdown and when you click it again it should change to .hidden

.dropdown {
position: relative;
top: 25px;
margin: 0 auto;
padding: 5px 0 0 0;
width: 200px;
height: auto;
background: $primary_color1_white;
border: 1px solid $primary_color2_black;
border-radius: 3px;
list-style: none;

&:before {
content: "";
border-width: 1px 0 0 1px;
border-style: solid;
border-color: #A1A4AA;
background: $primary_color1_white;
height: 14px;
width: 14px;
position: absolute;
top: -7px;
transform: translateX(-50%) rotate(45deg);
}

a {
text-decoration: none;
color: $primary_color2_black;
font-size: 1em;

li {
  text-align: left;
  padding:  10px 15px;
  margin: 0;

  i {
    margin-right: 10px;
  }
}
&:hover {
  color: #ffffff;
  li {
    background: linear-gradient(to top right, $primary_color1_white, $melon      30%);  } } } }
.hidden{visibility: hidden;}

in your template modify this :

<div class="add-button" dropdown>
  <div (click)="toggleDropdown()"> +</div>

  <ul class="dropdown" *ngIf="showDropdown">

    <a (click)="toggleDropdown()" [routerLink]="['/account']" href="leagues/account.component.html">
      <li><i class="fa fa-check-square-o fa-2"></i>Mijn account</li>
    </a>
    <a (click)="toggleDropdown()" [routerLink]="['/play']" href="leagues/play.component.html">
      <li><i class="fa fa-comments fa-2"></i>Plaats pronostiek</li>
    </a>
    <a (click)="toggleDropdown()" [routerLink]="['/leagues']" href="leagues/join-league.component.html">
      <li><i class="fa fa-clipboard fa-2"></i>Nieuwe competitie</li>
    </a>
    <a (click)="toggleDropdown()" href="">
      <li><i class="fa fa-user-plus fa-2"></i>Shop</li>
    </a>
    <a (click)="toggleDropdown()" href="">
      <li><i class="fa fa-user-plus fa-2"></i>Log out</li>
    </a>

  </ul>
</div>

and then in your component logic:

showDropdown: boolean = false;

toggleDropdown():void { 
   this.showDropdown = !this.showDropdown;
}

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