如何在角中隐藏引导程序模态 [英] How to hide bootstrap modal in angular

查看:87
本文介绍了如何在角中隐藏引导程序模态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用引导程序模态,但是当我将其从代码隐藏中隐藏时,它会显示以下错误.

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

我正在使用angular6.

I am using angular6.

代码

<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();

错误消息

错误TypeError:this.exampleModal.hide不是函数

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

推荐答案

您可以在模式对话框中添加关闭按钮HTML,并添加模板变量#closebutton

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

在ts代码中,您调用this.closebutton.nativeElement.click();关闭模式对话框.

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();
  }
}

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

更新:

我还有另一种解决方案,没有关闭按钮的窍门.

I have another solution without trick on close button.

第一步,需要通过npm命令安装jquerybootstrap.

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

第二,您需要在组件中添加declare var $ : any;

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

并且可以使用$('#myModal').modal('hide'); onSave()方法上

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

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

这篇关于如何在角中隐藏引导程序模态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆