简体   繁体   English

Angular Material Expansion 面板,仅在单击按钮时展开

[英]Angular Material Expansion panel, expand only on button click

By default Angular expansion panel expands when user clicks anywhere in the title.默认情况下,当用户单击标题中的任意位置时,Angular 扩展面板会展开。 However I want to update this functionality so that only right side arrow button should trigger an expansion event.但是我想更新此功能,以便只有右侧箭头按钮才能触发扩展事件。 Can someone help me for achieving this?有人可以帮助我实现这一目标吗? Below is a sample code.下面是一个示例代码。 https://stackblitz.com/edit/angular-exp-panel-click https://stackblitz.com/edit/angular-exp-panel-click

<button (click)="panel1.toggle()" mat-raised-button>Toggle panel 1</button>
<button (click)="panel2.toggle()" mat-raised-button>Toggle panel 2</button>

Buttons outside mat-accordion are properly expanding desired panels, however same buttons when added inside title are not working as expected. mat-accordion 外部的按钮正在正确扩展所需的面板,但是在标题中添加的相同按钮无法按预期工作。

You can either:您可以:

  1. Remove the click event handler completely.完全删除click事件处理程序。 This works because the button catches the mouse click, while the rest of the panel doesn't since it has the pointer-events: none style attribute.这是有效的,因为按钮捕获鼠标单击,而面板的其余部分没有,因为它具有pointer-events: none样式属性。 The click event is then propagated to the panel, which toggles the expansion.然后将单击事件传播到面板,从而切换展开。
<button class="toggle-panel" mat-raised-button>
  1. Call $event.stopPropagation() in the event handler, to prevent the panel from also receiving the click event, which would toggle the expansion back to its original state.在事件处理程序中调用$event.stopPropagation() ,以防止面板也接收单击事件,这会将扩展切换回其原始状态。
<button class="toggle-panel" (click)="$event.stopPropagation(); panel2.toggle()" ... >

See this stackblitz for a demo.有关演示,请参阅此 stackblitz

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

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