javascript - 直接向 mat-step 添加(点击)事件处理程序?

标签 javascript angular typescript angular-material

是否可以直接向 mat-step 元素添加点击事件处理程序?

我尝试在此演示中的第 2 步 中添加一个,但我没有看到记录的点击:

我尝试过的代码:

HTML:

<mat-step label="Step 2" (click)="click()" optional>
 // Other stuff
</mat-step>

TS:

click() {
  console.log("CLICKED STEP 2")
}

Stackblitz Demo重现该行为。

相关的 Angular Material 问题

https://github.com/angular/components/issues/18080

仅当第 1 步完成时才有效

最佳答案

您可以使用(selectionChange)父标签的事件为 mat-h​​orizo​​ntal-stepper

HTML:

<mat-horizontal-stepper linear (selectionChange)="selectionChange($event)">
    <mat-step label="Step 1" [completed]="step1Complete">
        <h4>Step 1</h4>
        <button (click)="toggleStep1Complete()" mat-flat-button color="primary">{{ step1Complete === true ? "Toggle:Complete" : "Toggle:Incomplete"}}
</button>
        <div>
            <button mat-button matStepperNext>Next</button>
        </div>
    </mat-step>
    <mat-step label="Step 2" optional>
        <p>Step 2</p>
        <div>
            <button mat-button matStepperPrevious>Back</button>
            <button mat-button matStepperNext>Next</button>
        </div>
    </mat-step>
    <mat-step label="Step 3">
        <p>Step 3</p>
    </mat-step>
</mat-horizontal-stepper>

TS 代码:

import { StepperSelectionEvent } from '@angular/cdk/stepper'; -- Better to use to get an intellisense about properties and available methods

selectionChange(event: StepperSelectionEvent) {
  console.log(event.selectedStep.label);
  let stepLabel = event.selectedStep.label;
  if (stepLabel == "Step 2") {
    console.log("CLICKED STEP 2");
  }
}

A working demo

EDIT:

如果您希望每个步骤都可点击或可供用户使用,那么您必须删除 mat-h​​orizo​​ntal-stepperlinear 属性或将其设置为

Documentation

关于javascript - 直接向 mat-step 添加(点击)事件处理程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59549423/

相关文章:

javascript - 在 texbox 将其放入之前/之后追加 div

javascript - 如何在表格的每个单元格中放置一个不可见的输入

angular - 如何通过单击angular2中的发送按钮来显示错误

typescript - 如何为对象数组定义接口(interface)?

angularjs - 如何使 Typescript 与 SystemJS 和 Angular 一起使用成为可能?

javascript - 如何使用javascript链表实现单索引数组

javascript - 无法使用 Renderer2 更改使用 ViewChild 引用的按钮的属性

angular - 如何清除 Angular react 形式的 FormArray 值?

typescript - typescript如何像js数组中的map函数一样转换union类型的各个类型

javascript - js - appendChild 方法破坏整个页面