这是我为此使用的一种方法,将每个步骤的内容投影到另一个组件中。这避免了使用服务、事件处理程序等。
就我而言,我想将当前步骤投影到步进器旁边的扩展面板中。此示例说明了如何使用面板操作行中的按钮来导航步进器,从而从步进器中填充它的各个部分:
<mat-stepper #stepper orientation="vertical">
<ng-template #expansionPanelHeader>
<ng-container [ngTemplateOutlet]="stepper.selected?.stepLabel.template"></ng-container>
</ng-template>
<ng-template #expansionPanelContent>
<ng-container [ngTemplateOutlet]="stepper.selected?.content"></ng-container>
</ng-template>
<ng-template #expansionPanelActionRow>
<button mat-button matStepperPrevious>Back</button>
<button mat-button matStepperNext>Next</button>
</ng-template>
<mat-step>
<ng-template matStepLabel>Step 1</ng-template>
<ng-template matStepContent>
This is the content of step 1.
</ng-template>
</mat-step>
<mat-step>
<ng-template matStepLabel>Step 2</ng-template>
<ng-template matStepContent>
This is the content of step 2.
</ng-template>
</mat-step>
</mat-stepper>
<!-- The current step will be projected into this component -->
<mat-expansion-panel>
<mat-expansion-panel-header>
<ng-container [ngTemplateOutlet]="expansionPanelHeader"></ng-container>
</mat-expansion-panel-header>
<ng-template matExpansionPanelContent>
<ng-container [ngTemplateOutlet]="expansionPanelContent"></ng-container>
</ng-template>
<mat-action-row>
<ng-container [ngTemplateOutlet]="expansionPanelActionRow"></ng-container>
</mat-action-row>
</mat-expansion-panel>
不幸的是,我能找到防止步进器内容也出现在步进器内部的唯一方法是这段 CSS:
.mat-vertical-content-container {
display: none !important;
}
CSS 需要放置在全局.scss
文件中,或者您需要使用ng-deep
或其他类似的“解决方案”。我更喜欢前者,仅限于包含的元素。