问题是它何时呈现的mat-step
更改mat-step-header
以及它上面的任何自定义类或属性都将消失。
以下代码有效,但很混乱。最好的解决方案是找到另一个具有您需要的向导组件,或者在 GitHub 上向 Material 开发人员提交请求,以将隐藏标志添加到mat-step
.
StackBlitz
班级:
export class AppComponent implements OnInit, AfterViewInit{
private ngVersion: string = VERSION.full;
MAX_STEP = 7;
@ViewChild('stepper') private myStepper: MatStepper;
step: number = 0;
page:number = 0;
constructor() {}
ngOnInit() {}
ngAfterViewInit() {
this.rerender();
}
goBack() {
if (this.step > 0) {
this.step--;
this.myStepper.previous();
}
this.page = this.step > 3 ? 1 : 0;
this.rerender();
}
goForward() {
if(this.step < this.MAX_STEP) {
this.step++;
this.myStepper.next();
}
this.page = this.step > 3 ? 1 : 0;
this.rerender()
}
private rerender() {
let headers = document.getElementsByTagName('mat-step-header');
let lines = document.getElementsByClassName('mat-stepper-horizontal-line');
for (let h of headers) {
if (this.page === 0) {
if (Number.parseInt(h.getAttribute('ng-reflect-index')) > 3) {
h.style.display = 'none';
}
else {
h.style.display = 'flex';
}
}
else if (this.page === 1) {
if (Number.parseInt(h.getAttribute('ng-reflect-index')) < 4) {
h.style.display = 'none';
}
else {
h.style.display = 'flex';
}
}
}
for (let i = 0; i < lines.length; i++) {
if (this.page === 0) {
if (i > 2) {
lines[i].style.display = 'none';
}
else {
lines[i].style.display = 'block';
}
}
else if (this.page === 1) {
if (i < 4) {
lines[i].style.display = 'none';
}
else {
lines[i].style.display = 'block';
}
}
}
}
}
看法:
<div class="solution">
<!--------------------------------------------------------------------------------------->
<mat-horizontal-stepper #stepper>
<mat-step>
Step 1
</mat-step>
<mat-step>
Step 2
<input matInput placeholder="Address" required>
</mat-step>
<mat-step>
Step 3
</mat-step>
<mat-step>
Step 4
</mat-step>
<mat-step>
Step 5
</mat-step>
<mat-step>
Step 6
<input matInput placeholder="Address" required>
</mat-step>
<mat-step>
Step 7
</mat-step>
<mat-step>
Step 8
</mat-step>
<!-- one option -->
</mat-horizontal-stepper>
<!-- second option -->
<div>
<button (click)="goBack()" type="button" [hidden]="step === 0">Back</button>
<button (click)="goForward()" type="button" [hidden]="step === MAX_STEP">Next</button>
</div>
<!--------------------------------------------------------------------------------------->
Step: {{step}}
<br>
Page: {{page}}
</div>