如何将Angular Material中的材料步进器(material stepper)默认打开第二项并设置图标颜色?

3
我使用了一个垂直材料步进器并希望默认打开第二个项目。现在它会打开第一个项目。我想默认打开“填写您的地址”。为此,我使用了Material Angular。因此,当我打开应用程序时,我希望打开第二个项目并更改图标颜色。我附上了代码。任何帮助都将不胜感激。
html:
<button mat-raised-button (click)="isLinear = !isLinear" id="toggle-linear">
  {{!isLinear ? 'Enable linear mode' : 'Disable linear mode'}}
</button>
<mat-vertical-stepper [linear]="isLinear" #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">
    <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-vertical-stepper>

TS:

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

/**
 * @title Stepper vertical
 */
@Component({
  selector: 'stepper-vertical-example',
  templateUrl: 'stepper-vertical-example.html',
  styleUrls: ['stepper-vertical-example.css']
})
export class StepperVerticalExample implements OnInit {
  isLinear = false;
  firstFormGroup: FormGroup;
  secondFormGroup: FormGroup;

  constructor(private _formBuilder: FormBuilder) {}

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

7

在步进控件上将selectedIndex设置为1

HTML代码

 <mat-horizontal-stepper  #stepper [selectedIndex]="1" (selectionChange)="stepChange($event)">
         <!-- Your code -->
 </mat-horizontal-stepper>

TS 代码

public selectedIndex: number;
public iconColor: stirng;
stepChange(event){
    this.selectedIndex= event.selectedIndex;
    if(event.selectedIndex === 0){
        this.iconColor = 'your color';
    }
}

在HTML中,如果你想设置颜色,可以这样做:

Or

<mat-icon [color]="selectedIndex === 0 ? 'primary' : 'warn'"></mat-icon>

或者
<mat-icon [color]="getColor()"></mat-icon>



 getColor(){
    return selectedIndex === 0 ? 'primary' : 'warn'
 }

我更新了问题。我能否更改默认打开项目的背景图标颜色? - ryy77
我该如何在TypeScript中设置它并为图标添加颜色? - ryy77
你好!我在这里发布了一个问题https://stackoverflow.com/questions/55936137/how-to-set-2-default-open-items-in-material-stepperangular。你能帮助我吗? - ryy77
@AndreiGhervan,要在TypeScript中设置颜色,您可以创建一个函数并使用它。我已经更新了我的答案。 - khush

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接