To open bootstrap 4 modal by calling method in typescript

Viewed 24032

I am using bootstrap 4 modal in my angular app(v6):

CODE:

<div class="container">
  <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal">
    Open
  </button>

  <!-- The Modal -->
  <div class="modal" id="myModal">
    <div class="modal-dialog">
      <div class="modal-content">

        <!-- Modal Header -->
        <div class="modal-header">
          <h4 class="modal-title">Modal Heading</h4>
          <button type="button" class="close" data-dismiss="modal">&times;</button>
        </div>

        <!-- Modal body -->
        <div class="modal-body">
          Modal body..
        </div>

      </div>
    </div>
  </div>

</div>

Now i will open the modal by clicking open button. This works fine.But instead of opening it manually, On clicking open button i need call method/function for ex:

open() {
     if('Some condition'){
       // Don't open the modal
     } else {
        // Open the modal
     }

   }

In the open() method if condition fails modal should open, else should not.How can i achieve this using only typescript

Stackblitz Demo

3 Answers

You need to use @ViewChild directive to achieve this. Just add below code :

<!-- The Modal -->
  <div class="modal" id="myModal"  #content>
    <div class="modal-dialog">
      ...... further code

in your component.ts file :

export class AppComponent implements OnInit {

     @ViewChild('content') content: any;
     public  ngOnInit() {
  }

   public open() {
     if(!true){
       // Dont open the modal
     } else {
        // Open the modal
        this.content.open();
     }

   }

Updated stackblitz : https://stackblitz.com/edit/angular-ckggpk

You can also show do this using *ngIf to modal dialog with some boolean value.

import { Component, OnInit, ViewChild } from '@angular/core';
import {
  FormBuilder,
  FormGroup,
  Validators,
} from '@angular/forms';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent implements OnInit {

  showModalBox: boolean = false;

     public  ngOnInit() {
  }

   public open() {
     if(0){
       // Dont open the modal
       this.showModalBox = false;
     } else {
        // Open the modal
        this.showModalBox = true;
     }

   }
}
<div class="container">
  <button type="button" class="btn btn-primary" data-toggle="modal" (click)="open()" data-target="#myModal">
    Open modal
  </button>

  <!-- The Modal -->
  <div *ngIf="showModalBox" class="modal" id="myModal">
    <div class="modal-dialog">
      <div class="modal-content">
      
        <!-- Modal Header -->
        <div class="modal-header">
          <h4 class="modal-title">Modal Heading</h4>
          <button type="button" class="close" data-dismiss="modal">&times;</button>
        </div>
        
        <!-- Modal body -->
        <div class="modal-body">
          Modal body..
        </div>
        
      </div>
    </div>
  </div>
  
</div>

Stackblitz : https://stackblitz.com/edit/angular-2ngwu1

Related