Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 4 & Material Stepper

Tags:

Is it possible to reset (or just jump to the first step) inside of a stepper? It is not documented in the docs, but is it possible to do so? It is stated in the docs that the stepper is built on "CDK Stepper" (link?), but I can't find any examples which lead me to my goal.

like image 233
Ben jamin Avatar asked Sep 27 '17 06:09

Ben jamin


People also ask

What is the Angular 4?

Angular 4 is a JavaScript framework for building web applications and apps in JavaScript, html, and TypeScript, which is a superset of JavaScript. Angular provides built-in features for animation, http service, and materials which in turn has features such as auto-complete, navigation, toolbar, menus, etc.

Is Angular 4 Old?

Angular 4 was released on March 6, 2017, which is compatible with most of the applications. However, there is not any major changes in Angular 4 from Angular 2, and it offers better bug fixed, and alerts compare to Angular 2. Angular five was released in November 2017.

When did Angular 4 release?

Version 4. On 13 December 2016 Angular 4 was announced, skipping 3 to avoid a confusion due to the misalignment of the router package's version which was already distributed as v3. 3.0. The final version was released on March 23, 2017.

What is the difference between Angular 2 and 4?

is the latest version of Angular. Although Angular 2 was a complete rewrite of AngularJS, there are no major differences between Angular 2 and Angular 4. Angular 4 is only an improvement and is backward compatible with Angular 2.


2 Answers

Ok, it seems I found a solution (but it is not stated anywhere on the API).

  1. Add a reference to the stepper, then add ViewChild with the reference in your component typescript file.
  2. Create a method to change the index of the stepper.

HTML:

<mat-horizontal-stepper [linear]="true" #stepper>     <!-- Content here --> </mat-horizontal-stepper> 

TS:

import { Component, ViewChild } from '@angular/core'; @Component({     // ... }) export class AppComponent {     @ViewChild('stepper') stepper;      /**      * Changes the step to the index specified      * @param {number} index The index of the step      */     changeStep(index: number) {         this.stepper.selectedIndex = index;     } } 
like image 74
Ben jamin Avatar answered Sep 20 '22 12:09

Ben jamin


It is possible to jump to a specific stepper. MatStepper exposes a public property selectedIndex which specifies the currently selected step index. It can be set. The index starts at 0.

In your template:

Add an id to your stepper e.g. #stepper. Then add a button in your step, and pass the stepper id in a function on (click) like below:

<mat-horizontal-stepper [linear]="isLinear" #stepper>     <!-- Your other steps here -->     <mat-step [stepControl]="secondFormGroup">         <!-- Content in the step -->         <div>             <!-- Other actions here -->                              <button mat-button (click)="resetStepper(stepper)">Reset</button>         </div>     </mat-step>     <!-- More steps here --> </mat-horizontal-stepper> 

.. and in your typescript:

import { MatStepper } from '@angular/material';  exported YourOwnComponent {    constructor() {}    resetStepper(stepper: MatStepper){      stepper.selectedIndex = 0;   } } 

Stackblitz demo

like image 45
Faisal Avatar answered Sep 17 '22 12:09

Faisal