如何使用jQuery隐藏带有Angular的boostrap4模态?

时间:2018-04-26 02:29:50

标签: jquery angular

我在包中添加了jquery,boootstrap。 JQuery工作正常,但模态功能不起作用。

ts& HTML



import $ from 'jquery';
export class AppComponent {
  name = 'Angular 5';

  closeModal() {
    $("#exampleModal").modal('toggle');
  }
}

<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
      Launch demo modal
    </button>

<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
              <span aria-hidden="true">&times;</span>
            </button>
      </div>
      <div class="modal-body">
        ...
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary" (click)="closeModal()">Save changes</button>
      </div>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;

错误

TypeError: jquery_1.default(...).modal is not a function

现场演示

https://stackblitz.com/edit/angular-jquery-modal

1 个答案:

答案 0 :(得分:2)

要在Typescript中使用jQuery,你必须这样做。 必须在所有导入后放置该行。

//import $ from 'jquery';

 declare var $:any;

 export class AppComponent {
   name = 'Angular 5';

   closeModal() {
     $("#exampleModal").modal('toggle');
   }
 }