2

我正在使用角度 7 中的 CDKStepper 创建自定义步进器,

我在用

<cdk-step>
      <ng-template cdkStepLabel>Fill out your name 1</ng-template>
      <p>This is any content of "Step 1"</p>
</cdk-step>

并在步进模板中 - 用于导航,我正在使用

  <ul class="nav nav-pills wizard-navigation-ul">
      <li class="step" *ngFor="let step of steps; let i = index;" [ngClass]="{'active': selectedIndex === i}">
          <a href="javascript:void()" (click)="onClick(i, step)" data-toggle="tab">{{step.stepLabel}}</a>
      </li>
  </ul>

并在 Component.ts

onClick(index: number, step: any): void {
    console.log(step);   // here i want to console the title of the step clicked, in this case TEXT of this "<ng-template cdkStepLabel>Fill out your name 1</ng-template>"
    this.selectedIndex = index;
  }

如何获取存储在中的标题<ng-template cdkStepLabel>Fill out your name 1</ng-template>

4

1 回答 1

1

遇到了这个,不得不深入研究源代码以获得解决方案。

CdkStepLabel指令具有TemplateRef公开的。因此,我们可以在CdkStepper子组件中执行以下操作:

<ng-container [ngTemplateOutlet]="selected.stepLabel.template"></ng-container>

(但是您想访问正确的步骤取决于您)

希望这对未来的搜索者有所帮助。

于 2019-12-02T19:18:57.293 回答