Angular has come a long way after its version 2, and now with many more libraries and tools to help with the development than ever before. In this article, I will create a demo to make you understand about how we can create custom modal in angular using angular material using Angular-CLI.
Let’s Get Started
ng new angular7-custom-modal
npm install --save @angular/material @angular/cdk
ng g c home
Open the home.component.ts file and update the below code in it.
import { Component , OnInit,Inject } from '@angular/core';
import {MatDialog, MAT_DIALOG_DATA} from '@angular/material';
export interface DialogData {
user: 'user A' | 'user B' | 'user C';
}
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {
title = 'Angular 7 Custom Modal';
constructor(public dialog: MatDialog) { }
ngOnInit() {
}
openDialog() {
this.dialog.open(MainChatModalComponent, {
data: {
user: 'user A'
}
});
}
}
@Component({
selector: './main-chat-modal',
templateUrl: './main-chat-modal.html',
})
export class MainChatModalComponent {
constructor(@Inject(MAT_DIALOG_DATA) public data: DialogData) {}
}
For the simplicity, I am doing everything in very less file, You can see I have created Chat modal component here only. Normally while development we should create this as service so that it can be used at multiple component. But here I am going to make everything in very briefly.
As I told just before that I will go in very brief way, So create a file inside home componet folder named main-chat-modal.html and put the below code in it.
<h1 mat-dialog-title>Chat list</h1>
<div mat-dialog-content>
<mat-dialog-actions>
<button mat-button [mat-dialog-close]="data.user">Close</button>
</mat-dialog-actions>
Chat user list:
<ul>
<li>
<span *ngIf="data.user === 'user A'">✓</span> user A
</li>
<li>
<span *ngIf="data.user === 'user B'">✓</span> user B
</li>
<li>
<span *ngIf="data.user === 'user C'">✓</span> user C
</li>
</ul>
</div>
Import below file in styles.css file placed under src folder.
@import '~@angular/material/prebuilt-themes/indigo-pink.css';
Open home.component.html file and put below code inside it
<div style="text-align:center;">
<a mat-button (click)="openDialog()">Open dialog</a>
</div>
Open app-routing.modal.ts file
const routes: Routes = [ { path: '', component: HomeComponent }];
Open app.module.ts file and put below code
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { MatDialogModule, MatDialogRef } from '@angular/material/dialog';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { HomeComponent,MainChatModalComponent } from './home/home.component';
@NgModule({
declarations: [
AppComponent,
MainChatModalComponent,
HomeComponent
],
imports: [
BrowserModule,
AppRoutingModule,
MatDialogModule,
BrowserAnimationsModule
],
providers: [ {provide: MatDialogRef
}],
entryComponents: [MainChatModalComponent],
bootstrap: [AppComponent]
})
export class AppModule { }
Run the app with ng serve over terminal.
So in this demo, We created a very basic demo to use the** Angular material library** for angular custom modal.
#angular