How to Create a Modal Popup for Angular?
I hope you are enjoying with this post! Please share with you friends. Thank you so much!
Two ways to CREAT Modal Popup Window -
1. Modal Popup using Typescript and Bootstrap
2. Modal Popup using Angular Material Dialogue
Open Modal Popup Using Typescript and Bootstrap –
Download and use the Bootstrap CDN to deliver Bootstrap's compiled CSS and JS to your project.
Steps 1 – Add Bootstrap CSS and Js files in the AppComponent.HTML file.
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" crossorigin="anonymous"></script>
You can also Install Bootstrap from NPM –
npm install bootstrap –save
Steps 2 – Add Style CSS for Login and Popup validations and it looks like.
.ng-valid[required], .ng-valid.required {
font-size: 16px;
text-align: center;
text-decoration: none;
border-left: 10px solid rgb(218, 200, 43); /* yellow */
}
.ng-invalid:not(form) {
font-size: 16px;
text-align: center;
text-decoration: none;
border-left: 10px solid #e94646; /* red */
}
.backdrop {
background-color:rgba(0,0,0,0.6);
position:fixed;
top:0;
left:0;
width:100%;
height:100vh;
}
Steps 3 – Added HTML for Open Popup – header, content body and footer to display and it looks like.
<!-- After clicking the button open the modal dialog using open/close event binding methods -->
<button type="button" class="btn btn-info btn-lg" (click)="closeModalDialog()">Open Login Modal Dialog!</button>
<div class="backdrop" [ngStyle]="{'display':display}"></div>
<!-- modal -->
<div class="modal" tabindex="-1" role="dialog" [ngStyle]="{'display':display}">
<!-- modal-dialog -->
<div class="modal-dialog" role="document">
<!-- modal-content -->
<div class="modal-content">
<!-- modal-header -->
<div class="modal-header">
<button type="button" class="close" aria-label="Close" (click)="openModalDialog()"><span aria-hidden="true">×</span></button>
<!-- <h4 class="modal-title">Login</h4> -->
</div>
<!-- modal-body -->
<div class="modal-body">
<form [formGroup]="logedInForm" (ngSubmit) = "mdfLogin(logedInForm.value)" >
<div>
<input type="text" class="textbox" name="emailId" placeholder="Email" formControlName="emailId">
</div>
<div>
<input type="password" class="textbox" name="password" placeholder="password" formControlName="password">
</div>
<div>
<input type="submit" class="btn btn-info btn-lg" value="LogIn" [disabled] = "!logedInForm.valid">
</div>
<div>
<p>EmailId Is - {{emailId}} and Password is - {{password}}</p>
</div>
</form>
</div>
<!-- modal-footer -->
<div class="modal-footer">
<button type="button" class="btn btn-default" (click)="closeModalDialog()" >Close</button>
</div>
</div>
</div>
</div>
In the above HTML, the methods openModalDialog() is used for open the popup and the method closeModalDialog() is for close popup and also method mdfLogin() is used for submit the login form.
Steps 4 - The Events looks like – user.component.ts
import { Component, OnInit } from '@angular/core';
import {FormGroup, FormControl, Validators} from '@angular/forms';
import { stringify } from '@angular/core/src/render3/util';
import { variable } from '@angular/compiler/src/output/output_ast';
@Component({
selector: 'app-user',
templateUrl: './user.component.html',
styleUrls: ['./user.component.css']
})
export class UserComponent implements OnInit {
date; //date Variable
logedInForm; //These are variables
emailId;
password;
display='none'; //default Variable
constructor() { }
ngOnInit() {
this.date = new Date(); // Today date and time
//Login Validation
this.logedInForm = new FormGroup({
emailId: new FormControl("youremail@gmail.com",
Validators.compose([
Validators.required,
Validators.pattern("[^ @]*@[^ @]*")
])),
password: new FormControl('YourPassword', [
Validators.minLength(8),
Validators.required])
});
}
// Model Driven Form - login
mdfLogin(data) {
this.emailId = data.emailId;
this.password = data.password;
alert(JSON.stringify(data));
}
openModalDialog(){
this.display='block'; //Set block css
}
closeModalDialog(){
this.display='none'; //set none css after close dialog
}
}
The result looks like –
The Video link is - https://youtu.be/Yb1YwEhUZmw
I hope you are enjoying with this post! Please share with you friends. Thank you so much!