3

我无法设置图标颜色

我有一个带有五个垫子台阶的垫子水平步进器(例如,命名为 A 部分、B 部分 ... E 部分)。根据某些业务规则,每个部分(mat-step)可能有不同的颜色。

我知道如何更改“选定的”垫子步骤或如何更改所有垫子步骤的颜色(颜色相同),但不知道如何动态更改它,因此每个部分可能有不同的图标背景颜色.

使用 Angular v7

以下是将第三个垫步图标设置为绿色的样式。Id 确实有效,但我不知道如何在运行时从组件(打字稿)动态更改颜色。

::ng-deep .mat-step-header:nth-of-type(3) .mat-step-icon         {
    background-color: green!important; 
 } 

我也尝试过使用[ngClass],但在用作 mat-step 属性时会被忽略。仅当我将步骤标签附在其中并在其中使用它时才有效,但这不是必需的(我需要更改图标的背景颜色......而不是标签)。

预期结果:能够根据每个步骤的完成程度为每个步骤设置不同的列。(垫步可以是黄色、红色、绿色和黑色的组合),

实际结果:无法根据组件变量设置更改图标颜色

4

1 回答 1

4

问题实际上是关于通过 TS 控制 CSS 变量——我从这里的这篇文章中得到了帮助;

  • CSS:我们将 3 个变量分配给我们需要着色的 3 个图标
  • HTML:除了 ,我们还创建了 2 个 div firstClass& ,我们可以为其分配唯一命名的变量,和;secondClass<body>color1color2color3
  • 由于我们使用的是'mat-table',我们不能使用[ngStyle]或者[ngClass]因为材质元素是在运行时创建的,我们只能对它们中的任何一个AfterViewInit事件进行操作,所以我们在这里为我们的 2 个 div 和<body>标签分配值;

相关的 CSS

::ng-deep .mat-step-header:nth-of-type(1) .mat-step-icon {
    background-color: var(--my-var1);
 } 

::ng-deep .mat-step-header:nth-of-type(2) .mat-step-icon {
    background-color: var(--my-var2);
 } 

::ng-deep .mat-step-header:nth-of-type(3) .mat-step-icon {
    background-color: var(--my-var3);
 } 

相关的 HTML

<div class='firstClass'>
    <div class=' secondClass'>
        <mat-horizontal-stepper labelPosition="bottom" #stepper>
            <mat-step [stepControl]="firstFormGroup">
                <form [formGroup]="firstFormGroup">
                    <ng-template matStepLabel>Fill out your name</ng-template>
                    <mat-form-field>
                        <input matInput placeholder="Last name, First name" formControlName="firstCtrl" required>
            </mat-form-field>
            <div>
                <button mat-button matStepperNext>Next</button>
            </div>
        </form>
    </mat-step>
    <mat-step [stepControl]="secondFormGroup" optional>
        <form [formGroup]="secondFormGroup">
            <ng-template matStepLabel>Fill out your address</ng-template>
            <mat-form-field>
                <input matInput placeholder="Address" formControlName="secondCtrl" required>
            </mat-form-field>
            <div>
                <button mat-button matStepperPrevious>Back</button>
                <button mat-button matStepperNext>Next</button>
            </div>
        </form>
    </mat-step>
    <mat-step>
        <ng-template matStepLabel>Done</ng-template>
        You are now done.
        <div>
            <button mat-button matStepperPrevious>Back</button>
            <button mat-button (click)="stepper.reset()">Reset</button>
        </div>
    </mat-step>
  </mat-horizontal-stepper>
  <div>
<div>

相关TS

import {Component, OnInit,AfterViewInit } from '@angular/core';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';

@Component({
  selector: 'stepper-label-position-bottom-example',
  templateUrl: 'stepper-label-position-bottom-example.html',
  styleUrls: ['stepper-label-position-bottom-example.css'],
})
export class StepperLabelPositionBottomExample implements OnInit, AfterViewInit {
  firstFormGroup: FormGroup;
  secondFormGroup: FormGroup;
  color1:string = 'green';
  color2:string = 'yellow';
  color3:string = 'red';

  constructor(private _formBuilder: FormBuilder) {}

  ngOnInit() {
    this.firstFormGroup = this._formBuilder.group({
      firstCtrl: ['', Validators.required]
    });
    this.secondFormGroup = this._formBuilder.group({
      secondCtrl: ['', Validators.required]
    });
  }

  ngAfterViewInit(){
    document.querySelector("body").style.cssText = "--my-var1: "+this.color1;
    (<HTMLElement>document.querySelector('.secondClass')).style.cssText = "--my-var2: "+this.color2;
    (<HTMLElement>document.querySelector('.firstClass')).style.cssText = "--my-var3: "+this.color3;
  }

}

在这里完成工作 stacblitz

于 2019-07-30T05:47:27.413 回答