angular - 如何在Angular中检测mat-stepper的开始和结束?

标签 angular typescript mat-stepper

目前,我有一个垂直垫步进器。它使用 *ngFor 填充步骤。

<mat-step [stepControl]="firstFormGroup" *ngFor="let stream of selectedStreamList; let indexOfelement = index;">

所以,我想检测这个步进器的开始和这个步进器的结束以用作变量。目前,我使用一个变量作为计数器。我增加和减少它以检测垫子步骤。但我想要一个很好的解决方案。那么有没有人可以帮助我解决这个问题?

最佳答案

每次步进更改时,mat-stepper 都会发出 selectionChange 事件。您可以在 typescript 文件中订阅这些更改并进行相应处理……可能是这样的。

//app.component.ts

import { Component, ViewChild, AfterViewInit } from "@angular/core";
import { MatStepper } from "@angular/material/stepper";
import { StepperSelectionEvent } from "@angular/cdk/stepper";
import { pluck } from "rxjs/operators";

@Component({
  selector: "app-root",
  template: `
    <mat-vertical-stepper #stepper>
      <mat-step
        *ngFor="let stream of selectedStreamList; let indexOfelement = index"
      ></mat-step>
    </mat-vertical-stepper>
  `
})
export class AppComponent implements AfterViewInit {
  @ViewChild("stepper", { static: false }) private stepper: MatStepper;

  selectedStreamList = [1, 2, 3, 4];
  isAtStart: boolean;
  isAtEnd: boolean;

  constructor() {}

  ngAfterViewInit() {
    this.stepper.selectionChange
      .pipe(pluck("selectedIndex"))
      .subscribe((res: number) => {
        this.isAtStart = res === 0;
        this.isAtEnd = res === this.selectedStreamList.length - 1;
      });
  }
}

关于angular - 如何在Angular中检测mat-stepper的开始和结束?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60974938/

相关文章:

javascript - 将数据传递到基于组件的 mat-stepper 中的下一步

angular - 'This' 在 subscribe 中未定义

javascript - 我们可以在没有 Node 的情况下使用 Angular 吗?

javascript - 向 mat-stepper 动态添加步骤并导航到它会抛出越界错误

angular - Mat Stepper 作为 View 子单元测试

typescript - 抽象类中的静态工厂方法

angular - 如何为通用组件提供更具体的组件以在 Angular 7 中进行渲染

javascript - 实现自定义验证器来 trim Angular 2 中的输入字段?

javascript - 两个数字不等于,但它们具有相同的类型和值

javascript - 从 Angular2/Typescript 中的 rxJs 复制响应数据