3

I use a vertical material stepper and I want to open by default the second item. Now it opens the first item. I would like to open "fill out your address" by default. For this I used Material Angular. So when I open the application I would like to open the second item and also to change the icon color. I attached the code. I would appreciate any help.

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]
    });
  }
}
ryy77
  • 1,134
  • 5
  • 20
  • 36

1 Answers1

7

Set selectedIndex to 1 on stepper

html code

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

ts code

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

Or

In html where you want to set color, you can do like this

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

Or

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



 getColor(){
    return selectedIndex === 0 ? 'primary' : 'warn'
 }
khush
  • 2,702
  • 2
  • 16
  • 35
  • I updated the question. Can i also change the background icon color of the default opened item? – ryy77 Apr 24 '19 at 14:55
  • How can I set this in typescript and add the color for the icon? – ryy77 Apr 24 '19 at 17:41
  • Hello! I put this question https://stackoverflow.com/questions/55936137/how-to-set-2-default-open-items-in-material-stepperangular here. Can you please help me? – ryy77 May 01 '19 at 16:05
  • @AndreiGhervan for setting color in typescript, you can create a function and use that instead. I have updated my answer. – khush May 02 '19 at 06:23