简体   繁体   English

显示/隐藏-按钮上的内容单击

[英]Show / Hide - Content on Button Click

Hello fellow programmer, 大家好,程序员,

im getting used to Angular 2, therefore Typescript, so have mercy with me. 我已经习惯了Angular 2,因此是Typescript,所以请怜悯我。

I have 5 Buttons which should enable or Disable Content on Click, like a Sidemenu. 我有5个按钮,应启用或禁用“单击内容”,例如“侧面菜单”。

HTML - Code HTML-代码

  <li class="navigation-items"> 
    <button class="dropdown-header" (click)="onSelect(2)">Link 2</button> 
    <div *ngIf="DropdownVar == 2" class="dropdown-content">
      <a href="#">Link 1</a>
      <a href="#">Link 2</a>
      <a href="#">Link 3</a>
    </div>
  </li>

Typescript - Code 打字稿-代码

DropdownVar = 0;

onSelect(x){
 this.DropdownVar = x;
 console.log(x);
}

It seems that my var gets the propper value but my *ngif does not work. 看来我的var获得了适当的值,但是我的*ngif无法正常工作。 Is there a better way to handle my Problem? 有没有更好的方法来解决我的问题?

Additional i would like to have a little animation for the Content which i want to show, but i guess css is the way to go. 另外,我想为内容展示一些动画,但是我想CSS是要走的路。

You can directly do it with (click)="DropdownVar=2" , no need of onSelect method 您可以直接使用(click)="DropdownVar=2" ,而无需onSelect方法

 <li class="navigation-items"> 
        <button class="dropdown-header" (click)="DropdownVar=2">Link 2</button> 
        <div *ngIf="DropdownVar === 2" class="dropdown-content">
          <a href="#">Link 1</a>
          <a href="#">Link 2</a>
          <a href="#">Link 3</a>
        </div>
      </li>

And use === instead of == in Angular2 并在Angular2中使用===代替==

you can use [hidden] instead ngIf 您可以使用[hidden]代替ngIf

<li class="navigation-items"> 
<button class="dropdown-header" (click)="onSelect()">Link 2</button> 
<div  class="dropdown-content" [hidden]="IsHidden">
  <a href="#">Link 1</a>
  <a href="#">Link 2</a>
  <a href="#">Link 3</a>
</div>

In Component 在组件中

IsHidden= true;

onSelect(){
 this.IsHidden= !this.IsHidden;
}

you can use [hidden] instead ngIf 您可以使用[hidden]代替ngIf

<li class="navigation-items"> 
    <button class="dropdown-header" (click)="onSelect()">Link 2</button> 
    <div  class="dropdown-content" [hidden]="IsHidden">
      <a href="#">Link 1</a>
      <a href="#">Link 2</a>
      <a href="#">Link 3</a>
    </div>

In Component 在组件中

IsHidden= true;

onSelect(){
this.IsHidden=true;
 this.IsHidden= !this.IsHidden;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM