【问题标题】:Angular Material Stepper, Dynamic color changeAngular Material Stepper,动态颜色变化
【发布时间】:2020-02-04 11:36:45
【问题描述】:

我正在尝试使用角度材料步进器作为进度跟踪器(我知道它不是为此而设计的......但这是唯一符合我要求的)。我的步进器中有 5 个步骤。

我想在产品或项目达到该点时打开颜色。 我为它找到了mat-step-icon-selected 类。但它不起作用。

我将从返回状态/步骤编号的 API 获取数据。 并基于此我需要改变颜色。

我无法更改班级的颜色。我尝试在mat-step 上使用ngClass 但它不起作用。

这是我的代码:

 <mat-horizontal-stepper [linear]="true">

    <mat-step>
        <ng-template matStepLabel >
          Initiated          
        </ng-template>
    </mat-step>
    <mat-step [className]="isTrue ? 'mat-step-icon-selected' : ''">
        <ng-template matStepLabel>PM</ng-template>
    </mat-step>
    <mat-step #stepper>
      <ng-template matStepLabel>HM</ng-template>
    </mat-step>
    <mat-step>
      <ng-template matStepLabel>BU</ng-template>
    </mat-step>
    <mat-step>
      <ng-template matStepLabel>FT</ng-template>
    </mat-step>
  </mat-horizontal-stepper> 

提出更新显示步进器颜色的解决方案。

谢谢:)

【问题讨论】:

    标签: angular angular-material angular-material-stepper


    【解决方案1】:

    将此添加到组件的 css 文件中:

    :host ::ng-deep .mat-step-icon-selected {
        color: green; // your styles
    }
    

    要动态设置步骤,请尝试如下:

    .ts

    import { MatStepper } from "@angular/material";
    
    @ViewChild("stepper") private stepper: MatStepper;
    
    selectStep(index) {
        this.stepper.selectedIndex = index;
    }
    

    .html

    <mat-horizontal-stepper [linear]="true" #stepper>
      ...
    </mat-horizontal-stepper>
    

    【讨论】:

    • 颜色正在改变..但仅适用于第一个...我希望它也适用于其他步进器
    • 它应该是我可以通过函数调用或 API 响应来改变特定步骤的颜色
    • 找到合适的类并像这样改变风格。我根据您在问题中提到的班级名称添加了这个
    • 我想将这个特定的类动态添加到步进器中
    • 我是 stackblitz 的新手,让我试试..btw...否则我会添加 questino 本身
    猜你喜欢
    • 1970-01-01
    • 2016-10-14
    • 1970-01-01
    • 2020-07-23
    • 2018-09-13
    • 1970-01-01
    • 2017-03-12
    • 2017-12-10
    • 1970-01-01
    相关资源
    最近更新 更多