AngularDoubts
AngularDoubts

Reputation: 499

ngbootstrap modal popup from component Typescript file in angular 5

I have the below code to open a modal on button click. How can I open the modal from a function inside the component ts file? My html is like this:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  <div>
  <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open The Modal Box</button>

  <!-- Modal -->
<div id="myModal" class="modal fade" role="dialog">
  <div class="modal-dialog">

    <!-- Modal content-->
    <div class="modal-content">
      <div class="modal-header">        
        <h4 class="modal-title">Sample Modal</h4>
        <button type="button" class="close" data-dismiss="modal">&times;</button>
      </div>
      <div class="modal-body">
        <p>Click OK to close.</p>
        <input [(ngModel)]="name"><br>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Ok</button>
      </div>
    </div>

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

The pop up should come from the component like this in component:

import { Component } from '@angular/core';

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

  OpenModalFun() {
      //Code to open modal pop-up
  }
  
  closeModalFun() {
      //Code to close modal pop-up
  }
 
}

I don't know how to open popup from TS file. Please help. Thanks in advance.

Upvotes: 0

Views: 6356

Answers (2)

Franklin Pious
Franklin Pious

Reputation: 3848

If you are using ngbootstrap you can easily do it from TS file

ngbootstrap modal popup example

https://ng-bootstrap.github.io/#/components/modal/examples

Upvotes: 1

לבני מלכה
לבני מלכה

Reputation: 16251

You can do it as bellow hide the button [hidden]="true":

<button id="openModalButton" type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal" [hidden]="true">Open The Modal Box</button>

And show modal from function (Simulate a button click):

import { Component } from '@angular/core';

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


  OpenModalFun() {
      document.getElementById("openModalButton").click();
  }

  closeModalFun() {
      //Code to close modal pop-up
  }

}

With angular use ElementRef

<button #btn id="openModalButton" type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal" [hidden]="true">Open The Modal Box</button>

And show modal from function (Simulate a button click):

import { Component } from '@angular/core';

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

  @ViewChild('btn') fileInput:ElementRef;
  OpenModalFun() {
      this.btn.nativeElement.click();
  }

  closeModalFun() {
      //Code to close modal pop-up
  }

}

Upvotes: 2

Related Questions