打开其他垫手风琴时如何关闭垫手风琴? [英] how to close a mat-accordion when other mat-accordion is opened?

查看:42
本文介绍了打开其他垫手风琴时如何关闭垫手风琴?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Angular v6 中使用 Angular Material.我有两个手风琴,每个手风琴都包含一个扩展面板.当我打开另一个手风琴时,如何关闭打开的手风琴?两台手风琴现在都保持开放!

I'm using Angular Material with Angular v6. I have two accordions where each accordion contains an expansion panel. How can I close the opened accordion when I open the other accordion? Both of the accordions stay open now!

代码:

<!-- first accordion -->
<mat-accordion>
    <mat-expansion-panel mat-list-item *ngIf="menuitem.type === 'sub'">
      <mat-expansion-panel-header class="main-header">
        <mat-panel-title>
          <mat-icon class="list-icon">{{ menuitem.icon }}</mat-icon>
          <span class="name">{{ menuitem.name }}</span>
        </mat-panel-title>
      </mat-expansion-panel-header>

      <mat-nav-list *ngFor="let child of menuitem.children" routerLinkActive="open">
        <a class="grand-menu" mat-list-item href="javascript:;" *ngIf="child.type === 'x'" [routerLink]="['/', menuitem.state, child.state ]">
          <span class="grand">{{ child.name }}</span>
        </a>
      </mat-nav-list>

      <mat-accordion open>
        <mat-expansion-panel mat-list-item *ngFor="let childitem of menuitem.children" routerLinkActive="open" #example>
          <mat-expansion-panel-header class="child" *ngIf="childitem.grand_children">
            <mat-panel-title class="child-title">
              <mat-icon class="arrow-icon" *ngIf="!example.expanded">keyboard_arrow_right</mat-icon>
              <mat-icon *ngIf="example.expanded">keyboard_arrow_down</mat-icon>
              {{ childitem.name }}
            </mat-panel-title>
          </mat-expansion-panel-header>

          <mat-nav-list *ngFor="let child of childitem.grand_children" routerLinkActive="open">
            <a class="grand-menu" mat-list-item href="javascript:;" *ngIf="childitem.type === 'parent'" [routerLink]="['/', menuitem.state, childitem.state, child.state ]">
              <span class="grand">{{ child.name }}</span>
            </a>
          </mat-nav-list>
        </mat-expansion-panel>
      </mat-accordion>
    </mat-expansion-panel>
  </mat-accordion>

   <!-- second accordion -->
  <mat-accordion>
      <mat-expansion-panel mat-list-item *ngIf="menuitem.type === 'sub1'">
        <mat-expansion-panel-header class="main-header">
          <mat-panel-title>
            <mat-icon class="list-icon">{{ menuitem.icon }}</mat-icon>
            <span class="name">{{ menuitem.name }}</span>
          </mat-panel-title>
        </mat-expansion-panel-header>

        <mat-nav-list *ngFor="let child of menuitem.children" routerLinkActive="open">
          <a class="grand-menu" mat-list-item href="javascript:;" *ngIf="child.type === 'x'" [routerLink]="['/', menuitem.state, child.state ]">
            <span class="grand">{{ child.name }}</span>
          </a>
        </mat-nav-list>
      </mat-expansion-panel>
    </mat-accordion>

推荐答案

使用 mat-expansion-panel 提供的打开和关闭的事件.

Use the opened and closed Events provided by mat-expansion-panel.

<mat-expansion-panel (closed)="isOpen = false" (opened)="isOpen = true">
  [...]
</mat-expansion-panel>

在您的班级中创建一个公共变量,例如是开放的:

In your class create a public variable, e.g. isOpen:

export class YourClass {
  isOpen: boolean;
}

然后将 isOpen 作为 @Input() 传递给另一个 mat-expansion-panel

Then pass isOpen as an @Input() to the other mat-expansion-panel

<mat-expansion-panel [expanded]="isOpen">
</mat-expansion-panel>

你必须在两个方向上实现这一点,以便让每个手风琴都能听到另一个.

You have to implement this in both directions in order to make each accordion listen to the other one.

这篇关于打开其他垫手风琴时如何关闭垫手风琴?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆