python ruby-programming-language Smalltalk Vue.js Knockoutjs TypeScript JavaScript, OOPs JavaScript Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 4 Angular 2 AngularJs 1.x NodeJs Perl Programming R Programming RequireJs Rust Programming Backbonejs closure programming language go-programming-language kotlin-programming-language
Angular 5 Modal Popup

Angular 5 and 4 - Open and Close Modal Popup Using Typescript and Bootstrap

How to Create a Modal Popup for Angular 5 and 4?

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 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 {
    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">&times;</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("[email protected]",
        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!
ANIL SINGH

Hi there! Welcome to my blog. My name is Anil Singh. I'm a author of this blog. For more detail about me, kindly refer to this link..
My Blogs - http://code-sample.com and http://code-sample.xyz
My Books - Interview Questions and Answers Books- Get Your Book in 15+ Digital Stores Worldwide..

You Might Also Like
www.code-sample.com/. Powered by Blogger.
ASK Questions