要展开特定id的mat-expansion panel,可以通过以下步骤实现:
<mat-accordion>
<mat-expansion-panel *ngFor="let panel of panels" [expanded]="panel.id === selectedId">
<mat-expansion-panel-header>
{{ panel.title }}
</mat-expansion-panel-header>
<p>
{{ panel.content }}
</p>
</mat-expansion-panel>
</mat-accordion>
selectedId: number | null = null;
togglePanel(id: number): void {
if (this.selectedId === id) {
this.selectedId = null; // 折叠面板
} else {
this.selectedId = id; // 展开面板
}
}
<mat-accordion>
<mat-expansion-panel *ngFor="let panel of panels" [expanded]="panel.id === selectedId">
<mat-expansion-panel-header (click)="togglePanel(panel.id)">
{{ panel.title }}
</mat-expansion-panel-header>
<p>
{{ panel.content }}
</p>
</mat-expansion-panel>
</mat-accordion>
通过以上步骤,你可以实现仅展开特定id的mat-expansion panel。当用户点击面板标题时,对应的面板将展开或折叠。你可以根据实际需求修改面板的数据和展开逻辑。
领取专属 10元无门槛券
手把手带您无忧上云