问题实际上是关于通过 TS 控制 CSS 变量——我从这里的这篇文章中得到了帮助;
- CSS:我们将 3 个变量分配给我们需要着色的 3 个图标
- HTML:除了 ,我们还创建了 2 个 div
firstClass
& ,我们可以为其分配唯一命名的变量,和;secondClass
<body>
color1
color2
color3
- 由于我们使用的是'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