【问题标题】:different component for each angular material stepper step via *ngFor通过 *ngFor 为每个角度材料步进步进的不同组件
【发布时间】:2020-10-25 16:57:36
【问题描述】:

我不知道如何循环遍历步进器的步骤列表,并使用 *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 做点什么,但我还不够熟悉,无法让它发挥作用。

【问题讨论】:

  • 所以基本上你希望在每个步骤中都有不同的组件,对吗?如果是这样,为什么要使用*ngFor
  • 是的。因为我还想让steps 通用。我想要一个步骤列表,其中包括它们的组件,并且能够创建不同的步进器。
  • 检查是否有帮助:netbasal.com/…

标签: angular angular-material


【解决方案1】:

您可以使用*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>

有关简化示例,请参阅 this stackblitz

【讨论】:

  • 嗨,我可以要求一个有效的 stackblitz 示例。我是新手,无法理解 *ngSwitchCase="0" [someInput0]="someInput0" (someOutput0)="onSomeOutput0($event)。就我而言,这些值是动态的,而不是 0,1、2 等
猜你喜欢
  • 2021-11-04
  • 2020-10-16
  • 1970-01-01
  • 1970-01-01
  • 2021-03-28
  • 2021-09-04
  • 2020-04-19
  • 1970-01-01
  • 2018-03-23
相关资源
最近更新 更多