ng2-dropdown菜单在angular2中不起作用

时间:2017-01-12 10:14:26

标签: javascript html angular navigation dropdown

我已经被困在这几个小时而且变得非常绝望。所以我有一个针对学校项目的angular2应用程序,但我想要做的是当我点击+按钮这个下拉菜单下来,当我再次点击它,或当我点击链接时,它再次关闭。现在它只是一直保持打开状态,当我点击它时什么都不做

我尝试过很多不同的东西,从ng2-dropdown到显示或隐藏菜单的javascript文件。我没有选择,对此感到非常沮丧。

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

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

我已经为ng2-dropdown模块做了适当的导入,这应该可以使它工作。它可能是一些小而愚蠢的东西,但我没有看到它。

这是我的app.component,它将导航栏作为模板。点击我路由到其他组件html。

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

这是我的css(实际上是sass,但在运行时会被转换)以用于下拉类以防万一。所以,当我点击按钮时,类是.dropdown,当你再次点击它时,它应该改为.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;}

1 个答案:

答案 0 :(得分:0)

在模板中

修改:

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

然后在你的组件逻辑中:

showDropdown: boolean = false;

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