简体   繁体   English

单击时关闭 Angular Mat-Expansion 面板

[英]Closing Angular Mat-Expansion Panel on Click

I have seen a few requests for toggling opened and closed Angular Material, but I am looking to always close it on the button click.我已经看到一些要求切换打开和关闭的 Angular Material,但我希望总是在单击按钮时关闭它。 So if the panel is opened and the button is clicked, then close the panel.因此,如果打开面板并单击按钮,则关闭面板。

Toggle works without any problem, but when I try to always close it (by setting the panelOpenState to false, it isn't seeming to work for me. Toggle 工作没有任何问题,但是当我尝试始终关闭它时(通过将panelOpenState设置为 false,它似乎对我不起作用。

In my html I have:在我的 html 中,我有:

<mat-expansion-panel [expanded]="panelOpenState">
    <mat-expansion-panel-header>
      <mat-panel-title>
        Workouts
      </mat-panel-title>
    </mat-expansion-panel-header>
   <a href="#">Create Workout</a>
  </mat-expansion-panel>

  <div>
    <div (click)="closePanel()">
      <h2>Training Plans </h2>
    </div>
  </div>

What I am trying to acheive is closing the panel when the div is clicked.我想要实现的是在单击 div 时关闭面板。 To do this, my click event in the ts file is as shown below:为此,我在ts文件中的点击事件如下所示:

...
export class SidenavComponent {
  panelOpenState: boolean = false;

  closePanel() {
    this.panelOpenState = false;
  }
}

On top of this, I have tried a few different methods within the closePanel() function.最重要的是,我在closePanel()函数中尝试了几种不同的方法。 The only method that has successfully worked is the toggle ( this.panelOpenState = !this.panelOpenState ).唯一成功的方法是切换( this.panelOpenState = !this.panelOpenState )。

How can I always close the panel?如何始终关闭面板? Setting it to false on click does not appear to work.单击时将其设置为 false 似乎不起作用。 The idea for me clicking on the div and having it close is because I want the panel to close any time I go to a different route.我点击 div 并关闭它的想法是因为我希望面板在我去不同的路线时关闭。

We can solve this by using two-way binding on the expanded attribute of mat-expansion-panel.我们可以通过在 mat-expansion-panel 的扩展属性上使用双向绑定来解决这个问题。

<mat-expansion-panel [(expanded)]="panelOpenState">
    <mat-expansion-panel-header>
    ...
    ...

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

相关问题 Angular 扩展面板在扩展模式下单击任意位置,调用 mat-expansion 单击事件 - Angular expansion panel click anywhere on expanded mode, invokes mat-expansion click event 设置禁用的Mat扩展面板的样式 - Set Styles for disabled Mat-Expansion Panel 如何动态添加扩展垫面板? - How to dynamically add mat-expansion panel? 格式化 mat-expansion 面板时遇到问题 - having trouble formatting mat-expansion panel 我们如何使用角度动画减慢 mat-expansion 面板的打开速度并赋予它们淡入淡出效果 - How can we slow down the opening of mat-expansion panel using angular-animations and give them a fade in out effect 角材料:垫子扩展内的桌子与其他桌子不对齐 - Angular material: table inside mat-expansion not aligned to other table 单击标题中的mat-checkbox时,防止mat-expansion面板切换 - Prevent mat-expansion panel from toggling when mat-checkbox in Header clicked 打开垫子扩展面板正在移动对面的柱子,如何保持不动? - Opening mat-expansion panel is moving the opposite column, how to keep it still? 如何在angular10中将mat-table放在mat-expansion中 - How to put mat-table inside mat-expansion in angular10 如何从 mat-expansion panel-body 获取输入数据? - How to get input data from mat-expansion panel-body?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM