Maddy
Maddy

Reputation: 31

How to hide bootstrap modal in angular

I am using bootstrap modal but when I hide it from code-behind it shows the error below.

I am using angular6.

Code

<div #exampleModal class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel"
        aria-hidden="true">

  @ViewChild('exampleModal') public exampleModal: ElementRef;

 this.exampleModal.hide();

Error message

ERROR TypeError: this.exampleModal.hide is not a function

Upvotes: 3

Views: 19930

Answers (4)

Mahdi
Mahdi

Reputation: 11

This is my solution:

html:

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

      <!-- Modal content-->
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h4 class="modal-title">Modal Header</h4>
        </div>
        <div class="modal-body">
            <p>Content here</p> 
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-danger" id="closeModal"  (click)="onSave()">Save</button>
         </div> 
      </div>
    </div>
  </div>

ts:

export class AppComponent implements OnInit {
  @ViewChild('myModal', { static: false }) myModal: ModalDirective;

  constructor(private fb: FormBuilder) { }

  public ngOnInit() {

  }

  public onSave() {
    this.myModal.hide();
  }
}

Upvotes: 1

Hien Nguyen
Hien Nguyen

Reputation: 18975

You can add close button HTML in your modal dialog and add template variable #closebutton

In ts code, you call this.closebutton.nativeElement.click(); to close modal dialog.

HTML

<div class="container">
  <h2>Modal Close</h2>
  <!-- Trigger the modal with a button -->
  <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open</button>

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

      <!-- Modal content-->
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" #closebutton class="close" data-dismiss="modal">&times;</button>
          <h4 class="modal-title">Modal Header</h4>
        </div>
        <div class="modal-body">
            <p>Content here</p> 
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-danger" id="closeModal"  (click)="onSave()">Save</button>
         </div> 
      </div>
    </div>
  </div>

</div>

TS

export class AppComponent implements OnInit {
  @ViewChild('closebutton') closebutton;

  constructor(private fb: FormBuilder) { }

  public ngOnInit() {

  }

  public onSave() {
    this.closebutton.nativeElement.click();
  }
}

Demo https://stackblitz.com/edit/angular-close-bootstrap-modal?file=src/app/app.component.html

Update:

I have another solution without trick on close button.

First step you need install jquery and bootstrap by npm command.

Second you need add declare var $ : any; in component

And use can use $('#myModal').modal('hide'); on onSave() method

Demo https://stackblitz.com/edit/angular-model-bootstrap-close?file=src/app/app.component.ts

Upvotes: 10

You can use ngx-bootstrap modal for controlling modal from Code. https://valor-software.com/ngx-bootstrap/#/modals

Upvotes: -1

Sachin Shah
Sachin Shah

Reputation: 4533

I have a another option for this which is very easy and works for me.

E.x: This is your model code in HTML file.

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

<!-- Modal content-->
<div class="modal-content">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" id="closeModel">&times;</button>
    <h4 class="modal-title">Modal Header</h4>
  </div>
  <div class="modal-body">
    <!-- Do your stuff -->
  </div>
  <div class="modal-footer">
    <button type="button" class="btn btn-default" data-dismiss="modal" >Close</button>


  </div>
</div>

Now in your .ts file where you want to close modal just use jquery for that.

Note: You need to install jquery for that and need to import in file.

Now click on close button by jquery.

$(document).ready(function(){    
    $("#closeModel").click();  // That's it... Model will close. 
});

Upvotes: 0

Related Questions