简体   繁体   English

如何绑定按钮以基于单击显示和隐藏

[英]How to bind a button to show and hide based on click

I have a list of headers (titles) and each has paragraphs... I created a list for this so that it has a button with icon on the right. 我有一个标题(标题)列表,每个标题都有段落...我为此创建了一个列表,以便它在右侧有一个带图标的按钮。 when the button is clicked , show the paragraph and when clicked again it should hide it back. 单击该按钮时,显示该段落,再次单击时应将其隐藏。

on the first click its shown but I don't know how to hide it back when its clicked again. 第一次单击时显示了它,但是再次单击时我不知道如何将其隐藏。

so far I have done something like this: 到目前为止,我已经做了类似的事情:

.html: html的:

<ion-list no-lines>
  <button ion-item (click)="itemClicked()">
    <h2>About {{details.company.data.company_name_en}}</h2>
     <ion-icon name="ios-arrow-down" item-right></ion-icon>
  </button>
  <div [style.visibility]= "toggoleShowHide">
    <p dir="auto" >
      {{details.company_profile}}
    </p>
  </div>
</ion-list>

.ts: .TS:

itemClicked(){
this.toggoleShowHide= 'visible';
}

Also , the space for the <div> tags are also not hidden.....And if someone can please suggest how to animate this properly..... when clicked the list should expand with the content inside , and then contract when clicked again. 此外, <div>标记的空间也不会隐藏。.....如果有人可以建议如何正确设置动画效果.....单击列表时,其内容应在其中扩展,然后在收缩时收缩再次点击。

For animations you can check out The Guide for Angular Animations 对于动画,您可以查看Angular动画指南

where you use animations to your component and can apply different styles to your animation. 在其中将animations应用于组件,并可以将不同的样式应用于动画。

With animations you can use [@triggerName] (name it as you want) to attach the animation to elements, and this is triggered by your click event. 对于动画,您可以使用[@triggerName] (根据需要命名)将动画附加到元素,这是由click事件触发的。 Then in your component you add the logic in animations , so it could look something like this: 然后在您的组件中添加animations的逻辑,因此看起来可能像这样:

  animations: [
    trigger('openClose', [
      state('collapsed, void',
        style({ height:"0px"})),
      state('expanded',
        style({ height:"*" })),
      transition("collapsed <=> expanded", [
        animate(500, style({ height: '250px'})),
        animate(500)
      ])
    ])
  ]

Here in this example we toggle the state between collapsed and expanded and apply styles and timing for openClose . 在此示例中,我们在collapsedexpanded之间切换状态,并为openClose应用样式和时间。 The above code does nothing special animation-wise. 上面的代码在动画方面没有做任何特殊的事情。 But that's where you come in and can apply whatever style you want! 但这就是您的用武之地,您可以应用所需的任何样式!

Here is a simple working example: 这是一个简单的工作示例:

Plunker Plunker

You can use the [hidden]="true/false" directive 您可以使用[hidden]="true/false"指令

ts : ts

this.toggoleShowHide = true;

itemClicked(){
  this.toggoleShowHide = !this.toggoleShowHide;
}

html: HTML:

<div [hidden]= "toggoleShowHide">
    <p dir="auto" >
      {{details.company_profile}}
    </p>
  </div>

This can be handled in html itself 这可以在html本身中处理

<ion-list no-lines>
 <button ion-button clear item-end class="roundButton" (click)="viewType = !viewType">
  <h2>About {{details.company.data.company_name_en}}</h2>
   <ion-icon name="ios-arrow-down" item-right></ion-icon>
 </button>
 <ion-item *ngIf="viewType">
  <ion-input type="text" value=""></ion-input>
 </ion-item>
</ion-list>

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

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