Solvedcomponents feat(stepper): support lazy rendering of steps
✔️Accepted Answer
Not sure if I am just stating the obvious, but I was able to implement lazy-loading based on @snebjorn 's approach quite easily:
<mat-vertical-stepper #stepper>
<mat-step #first_step>
<ng-container *ngIf="stepper.selected == null || stepper.selected == first_step">
</ng-container>
</mat-step>
<mat-step #second_step>
<ng-container *ngIf="stepper.selected == second_step">
</ng-container>
</mat-step>
<mat-step #third_step>
<ng-container *ngIf="stepper.selected == third_step">
</ng-container>
</mat-step>
</mat-vertical-stepper>
Note that the condition for the first step needs to include the check for null, too. Otherwise you'll receive an ExpressionChangedAfterItHasBeenCheckedError.
Other Answers:
Is there any update?
Yes, I need this as well for a project. I tried a number of approaches but they don't seem to be supported by the current implementation as the compiler throws errors or steps that are added later don't show up.
@matthiaswelz I have a linear form like below. How can i implement your approach in this?
<mat-horizontal-stepper [linear]="true" #stepper [selectedIndex]="0">
<mat-step [stepControl]="step1.stepFormGroup">
<ng-template matStepLabel>Step 1</ng-template>
<app-step-one #step1></app-step-one>
</mat-step>
<mat-step [stepControl]="step2.stepFormGroup">
<ng-template matStepLabel>Step 2</ng-template>
<app-step-two #step2 ></app-step-two>
</mat-step>
<mat-step [stepControl]="step3.stepFormGroup">
<ng-template matStepLabel>Step 3</ng-template>
<app-step-three #step3 ></app-step-three>
</mat-step>
<mat-step>
<ng-template matStepLabel>Done</ng-template>
You are now done.
<div>
<button mat-button matStepperPrevious>Back</button>
</div>
</mat-step>
</mat-horizontal-stepper>
If i try to add <app-step-one #step1></app-step-one>
inside ng-container
with *ngIf
then i am unable to get [stepControl]="step1.stepFormGroup"
from working.
Bug, feature request, or proposal:
feature request-ish
What is the expected behavior?
Each step in the stepper should have the option to be lazily rendered
What is the current behavior?
Currently every step is initialized when the stepper is loaded
What is the use-case or motivation for changing an existing behavior?
Inactive steps can have logic that fetches data or does some work that isn't relevant to the current step.
Currently there isn't any mention of how to achieve this in the stepper. However it can be done via
<ng-container *ngIf="condition">
, not sure if there are any side effects.The expansion panel supports lazy rendering "officially" https://material.angular.io/components/expansion/overview#lazy-rendering
Why is there a difference? If the
<ng-container>
is a perfectly fine solution why doesn't the expansion panel do it the same way? Why is there no mention of lazy rendering in the stepper doc?Which versions of Angular, Material, OS, TypeScript, browsers are affected?