每个角材料步进器步骤的不同组件通过 *ngFor



我不知道如何循环浏览步进器的步骤列表并使用*ngFor为每个步骤使用不同的组件。

我目前正在这样做:

<mat-horizontal-stepper
[linear]="true"
[labelPosition]="labelPosition"
(selectionChange)="onSelectionChange($event)"
[selectedIndex]="currentStep"
#stepper>
<mat-step [completed]="steps[0].completed" [label]="steps[0].label">
<app-my-component-0 [someInput0]="someInput0" (someOutput0)="onSomeOutput0($event)" *ngIf="currentStep === 0"></app-my-component-0>
</mat-step>
<mat-step [completed]="steps[1].completed" [label]="steps[1].label">
<app-my-component-1 [someInput1]="someInput1" (someOutput1)="onSomeOutput1($event)" *ngIf="currentStep === 1"></app-my-component-1>
</mat-step>
<mat-step [completed]="steps[2].completed" [label]="steps[2].label">
<app-my-component-2 [someInput2]="someInput2" (someOutput2)="onSomeOutput2($event)" *ngIf="currentStep === 2"></app-my-component-2>
</mat-step>
</mat-horizontal-stepper>

还有比这更优雅的方式吗?我想我需要对ngTemplate做点什么,但我不够熟悉,无法让它工作。

您可以使用*ngForngSwitch实现它,如下所示:

<mat-horizontal-stepper ...>
<mat-step *ngFor=let step of [0,1,2]" [ngSwitch]="step" [completed]="steps[step].completed" [label]="steps[step].label">
<app-my-component-0 *ngSwitchCase="0" [someInput0]="someInput0" (someOutput0)="onSomeOutput0($event)"></app-my-component-0>
<app-my-component-1 *ngSwitchCase="1" [someInput1]="someInput1" (someOutput1)="onSomeOutput1($event)"></app-my-component-1>
<app-my-component-2 *ngSwitchCase="2" [someInput2]="someInput2" (someOutput2)="onSomeOutput2($event)"></app-my-component-2>
</mat-step>
</mat-horizontal-stepper>

有关简化的示例,请参阅此堆栈闪电战。

最新更新