Angular Material is a popular UI framework based on Material Design for Angular.
In this article, we’ll look at how to use Angular Material into our Angular project.
Stepper
We can add steps display with the mat-horizontal-stepper
component.
For example, we can write:
app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { MatStepperModule } from '@angular/material/stepper';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatInputModule } from '@angular/material/input';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { MatButtonModule } from '@angular/material/button';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
AppRoutingModule,
BrowserAnimationsModule,
MatStepperModule,
MatFormFieldModule,
FormsModule,
ReactiveFormsModule,
MatInputModule,
MatButtonModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
app.component.ts
import { Component } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
firstFormGroup: FormGroup;
secondFormGroup: FormGroup;
isEditable = false;
constructor(private _formBuilder: FormBuilder) { }
ngOnInit() {
this.firstFormGroup = this._formBuilder.group({
firstCtrl: ['', Validators.required]
});
this.secondFormGroup = this._formBuilder.group({
secondCtrl: ['', Validators.required]
});
}
}
app.component.html
<div>
<mat-horizontal-stepper linear #stepper>
<mat-step [stepControl]="firstFormGroup" [editable]="true">
<form [formGroup]="firstFormGroup">
<ng-template matStepLabel>Fill out your name</ng-template>
<mat-form-field>
<mat-label>Name</mat-label>
<input matInput formControlName="firstCtrl"
placeholder="Name" required>
</mat-form-field>
<div>
<button mat-button matStepperNext>Next</button>
</div>
</form>
</mat-step>
<mat-step [stepControl]="secondFormGroup" [editable]="true">
<form [formGroup]="secondFormGroup">
<ng-template matStepLabel>Fill out your address</ng-template>
<mat-form-field>
<mat-label>Address</mat-label>
<input matInput formControlName="secondCtrl"
placeholder="Address" 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>
<p>You are now done.</p>
<div>
<button mat-button matStepperPrevious>Back</button>
<button mat-button (click)="stepper.reset()">Reset</button>
</div>
</mat-step>
</mat-horizontal-stepper>
</div>
We add the MatStepperModule
to let us add the mat-horizontal-stepper
and mat-step
components.
The MatFormFieldComponent
lets us add a form field.
The FormsModule
and ReactiveFormsModule
let us add the form validation to our app.
MatInputModule
lets us add inputs to mat-form-fields
with the matInput
directive.
MatButtonModule
lets us add Material Design style buttons.
In app.component.ts
, we add the reactive forms.
Then in app.component.html
, we add the mat-horizontal-stepper
to add the stepper.
The mat-step
components have the steps.
Each step has its own form with its own form field.
The editable
attribute makes the step editable.
The matStepperNext
directive makes the button go to the next step.
The formControlName
attribute sets the form control name to the ones defined in the component class so that we can add form validation.
We need to add the formGroup
attribute for this to work.
The stepper.reset
method resets the stepper state.
Conclusion
We can add a stepper with the mat-step
and mat-horizontal-stepper
components.