我在 ng-bootstrap 中使用 ngb-accordion。切换标题会导致其子组件重新初始化。例如,切换标题会导致下拉菜单重置。看起来这是因为<div class="card-block"></div>
面板隐藏时被删除。
即使组件“隐藏”在手风琴中,我如何维护组件的状态?
模板文件:
<ngb-accordion activeIds="side-bar-accordion-1" (panelChange)="beforeChange($event)">
<div [class.hide-card-block]="status">
<ngb-panel id="side-bar-accordion-1" class="foo">
<template ngbPanelTitle>
<div class="title">Axes</div>
</template>
<template ngbPanelContent>
<!--This is the component whose state I want to maintain:-->
<side-bar-axes></side-bar-axes>
</template>
</ngb-panel>
</div>
<ngb-panel id="side-bar-accordion-2">
<template ngbPanelTitle>
<div class="title">Fancy</div>
</template>
<template ngbPanelContent>
blah blah
</template>
</ngb-panel>
<ngb-panel id="side-bar-accordion-3">
<template ngbPanelTitle>
<div class="title">Settings</div>
</template>
<template ngbPanelContent>
blah blah
</template>
</ngb-panel>
</ngb-accordion>
组件 TypeScript 文件:
import { Component, ViewChildren, ViewEncapsulation } from '@angular/core';
import { NgbPanelChangeEvent } from '@ng-bootstrap/ng-bootstrap';
import { FieldChooseFiltersComponent } from '../field-choose-filters/field-choose-filters.component';
@Component({
moduleId: module.id,
selector: 'side-bar',
templateUrl: 'side-bar.component.html',
styleUrls: ['side-bar.component.css'],
encapsulation: ViewEncapsulation.None
})
export class SideBarComponent {
hideNum: number = 1;
status: boolean = false;
toggleStatus() {
this.status = !this.status;
}
public beforeChange($event: NgbPanelChangeEvent) {
if ($event.panelId === '1' && $event.nextState === false) {
$event.preventDefault();
}
if ($event.panelId === '2' && $event.nextState === false) {
$event.preventDefault();
}
if ($event.panelId === '3' && $event.nextState === false) {
$event.preventDefault();
}
};
}