A stepper is a type of input control that allows the user to increment or decrement
a numerical value using predefined steps. See how to create a stepper with lazy loaded steps in Angular.
First of all, we need to define how our component should look.
Because we want to lazy load steps individuals, we have to define router-outlet inside the stepper’s template.
To define which header is connected to which path, we have to also provide a path name to the appStepLabel directive.
Routes configuration
After adding router-outlet it is time to define routes. We do it like any other route. There is nothing special.
Do not forget that you can also implement canActivate, canDeactivate or even outlet.
Creating StepLabelDirective
The directive has just to carry a template and information for which path it is.
Whole logic will be placed in StepperComponent.
Building StepperComponent
Template
The template StepperComponent is very easy. It consists of two parts – displaying a header and content.
Logic
The component has to get defined labels and display in the template.
Label order is defined by the template. To change the order just reorder ng-template.
Highlight active step
Because the current step is saved in a router, we have to get the current index based on the path. To do it we should take the path from the route config.
Thanks to defining the variable we can highlight current and fulfilled steps. All that we need now is to add classes to the element based on logic.
Navigating between steps
Because steps are defined as a route component, we go through stepper just like between normal pages.
Do you like the content?
Your support helps me continue my work. Please consider making a donation.
Donations are accepted through PayPal or Stripe. You do not need a account to donate. All major credit cards are
accepted.