Angular Material From The Beginning - Part Three

In this article, we will discuss Angular Material Popups and Modal components.

Popups are used to display alerts and notification messages on the top of the current page.

In the previous parts of the series, we discussed basics of Angular material and Stepper component. You can check the previous articles here:

Angular Material provides the following types of Popups modal.

  1. Tooltips
  2. Dialog
  3. Snackbar
  4. Bottom sheet

Tooltips
Angular Material tooltip is a small pop-up box that is visible on mosue hovers or button click on an element. To create an Angular Material tooltip, we use the matTooltip property.

We can change the position of tooltip label. For that, we use the matTooltipPosition property.

Positions:

above : Show label above the element
below : Show label below the element
left : Show label to the left of the element
right : Show label to the right of the element

Step 1
Let’s create a new component by using the following command.

ng g c AngularMattooptips

Step 2
Now, open the app.module.ts file and import the required module for tooltip.

  1. import { BrowserModule } from '@angular/platform-browser';
  2. import { NgModule } from '@angular/core';
  3. import { AppRoutingModule } from './app-routing.module';
  4. import { AppComponent } from './app.component';
  5. import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
  6. import { FormsModule, ReactiveFormsModule } from '@angular/forms';
  7. import { AngularMatstepperComponent } from './angular-matstepper/angular-matstepper.component';
  8. import { MatStepperModule } from '@angular/material/stepper';
  9. import { MatButtonModule, MatFormFieldModule, MatInputModule } from "@angular/material";
  10. import 'hammerjs';
  11. import { AngularMattooptipsComponent } from './angular-mattooptips/angular-mattooptips.component'
  12. import {MatTooltipModule} from '@angular/material/tooltip';
  13. @NgModule({
  14. declarations: [
  15. AppComponent,
  16. AngularMatstepperComponent,AngularMattooptipsComponent
  17. ],
  18. imports: [
  19. BrowserModule, FormsModule, ReactiveFormsModule,MatTooltipModule,
  20. AppRoutingModule, BrowserAnimationsModule, MatStepperModule, MatButtonModule, MatFormFieldModule, MatInputModule
  21. ],
  22. providers: [],
  23. bootstrap: [AppComponent]
  24. })
  25. export class AppModule { }

Step 3

Now, open angular-mattooptips.component.html and add the following HTML.

  1. <div style="padding-top: 20px;padding-left:30px;">
  2. <button mat-stroked-button color="primary" [matTooltipPosition]="position" matTooltip="Click on Button">
  3. Button
  4. </button>
  5. </div>

Step 4
Now, open the angular-mattooptips.component.ts file and import TooltipPosition,add the following lines.

  1. import { Component, OnInit } from '@angular/core';
  2. import { TooltipPosition } from '@angular/material';
  3. @Component({
  4. selector: 'app-angular-mattooptips',
  5. templateUrl: './angular-mattooptips.component.html',
  6. styleUrls: ['./angular-mattooptips.component.css']
  7. })
  8. export class AngularMattooptipsComponent implements OnInit {
  9. constructor() { }
  10. position: TooltipPosition = 'before';
  11. ngOnInit() {
  12. }
  13. }

Step 5
Now, run the project mouse over on button and check the result.

Dialog:Angular material provide MatDialog module which used to open modal dialogs.

Step 6 Let’s create a component by using the following command.
ng g c AngularMatDialog

Step 7
Now, open the app.module.ts file and import MatDialog module.

  1. import { MatDialogModule } from '@angular/material';

Step 8
Now, open angular-mat-dialog.component.html and add the following HTML.

  1. <h1 mat-dialog-title>{{name}}</h1>
  2. <mat-dialog-content>Do you wish to Visit Jaipur ?</mat-dialog-content>
  3. <mat-dialog-actions>
  4. <button mat-button mat-dialog-close>No</button>
  5. <button mat-button [mat-dialog-close]>Yes</button>
  6. </mat-dialog-actions>

mat-dialog-title: This defines the title of the Dialog that has been applied with the heading elements h1, h2.
mat-dialog-content: contain body of the dialog.
mat-dialog-actions: contain buttons to confirm or close a dialog.

Step 9 Open angular-mat-dialog.component.ts and add the following code.

  1. import { Component, OnInit, Inject } from '@angular/core';
  2. import { MAT_DIALOG_DATA} from '@angular/material/dialog';
  3. export interface DialogData {
  4. animal: string;
  5. name: string;
  6. }
  7. @Component({
  8. selector: 'app-angular-mat-dialog',
  9. templateUrl: './angular-mat-dialog.component.html',
  10. styleUrls: ['./angular-mat-dialog.component.css']
  11. })
  12. export class AngularMatDialogComponent {
  13. name: string;
  14. constructor(@Inject(MAT_DIALOG_DATA) public data: any) {
  15. this.name = data.title;
  16. }
  17. }

Step 10 Open app.component.ts and import matdialog

  1. import { MatDialog, MatDialogConfig } from '@angular/material';

And, add the following code in app.component.ts file.

  1. import { Component } from '@angular/core';
  2. import { MatDialog, MatDialogConfig } from '@angular/material';
  3. import { AngularMatDialogComponent } from './angular-mat-dialog/angular-mat-dialog.component';
  4. @Component({
  5. selector: 'app-root',
  6. templateUrl: './app.component.html',
  7. styleUrls: ['./app.component.css']
  8. })
  9. export class AppComponent {
  10. title = 'app';
  11. constructor(public dialog: MatDialog) { }
  12. openModal() {
  13. const dialogConfig = new MatDialogConfig();
  14. dialogConfig.data = {
  15. title: 'Welcome To Jaipur'
  16. };
  17. const dialogRef = this.dialog.open(AngularMatDialogComponent, dialogConfig);
  18. }
  19. }

Step 11 Add the following HTML in the app.component.html file.

  1. <div style="padding:20px;">
  2. <button mat-raised-button color="primary" (click)="openModal()">Open Dialog</button>
  3. </div>

Now, run the code and check result.

Bottom Sheet

Bottom sheet is used to open a panel at the bottom of the screen. To implement a bottom sheet, we need to use the MatBottomSheet service from Angular Material.

Summary In this article, we learned about Angular Material popups and dialogs.