Angular 5 Modal Popup

Angular 8, 7, 6, 5, 4, 2 - Open and Close Modal Popup Using Typescript and Bootstrap

How to Create a Modal Popup for Angular?
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=""  crossorigin="anonymous">
<script src=""  crossorigin="anonymous"></script>

You can also Install Bootstrap from NPM –
npm install bootstrapsave

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 {

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">&times;</span></button>
        <!-- <h4 class="modal-title">Login</h4> -->

      <!-- modal-body -->
      <div class="modal-body">
        <form [formGroup]="logedInForm" (ngSubmit) = "mdfLogin(logedInForm.value)" >
                <input type="text" class="textbox" name="emailId" placeholder="Email" formControlName="emailId">
                <input type="password" class="textbox" name="password" placeholder="password" formControlName="password">
                <input type="submit" class="btn btn-info btn-lg" value="LogIn" [disabled] = "!logedInForm.valid">
                <p>EmailId Is  - {{emailId}} and Password is - {{password}}</p>

      <!-- modal-footer -->
      <div class="modal-footer">
        <button type="button" class="btn btn-default" (click)="closeModalDialog()" >Close</button>

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';

  selector: 'app-user',
  templateUrl: './user.component.html',
  styleUrls: ['./user.component.css']
export class UserComponent implements OnInit {
  date; //date Variable
  logedInForm; //These are variables
  display='none'; //default Variable

  constructor() { }

  ngOnInit() { = new Date(); // Today date and time
    //Login Validation
    this.logedInForm = new FormGroup({
      emailId: new FormControl("",
          Validators.pattern("[^ @]*@[^ @]*")
      password: new FormControl('YourPassword', [

  // Model Driven Form - login
  mdfLogin(data) {
    this.emailId = data.emailId;
    this.password = data.password;

    this.display='block'; //Set block css

  this.display='none'; //set none css after close dialog
The result looks like –

The Video link is -

I hope you are enjoying with this post! Please share with you friends. Thank you so much!

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..

My Tech Blog -
My Books - Book 1 and Book 2 Powered by Blogger.