在angular7中实现引导模式对话框 [英] Implementing bootstrap modal dialog in angular7

查看:63
本文介绍了在angular7中实现引导模式对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在实现一个简单的引导程序模态对话框上停留了一段时间,并在大约10个不同的页面中找到了答案.考虑到我无法快速找到答案,也没有明确地我想分享自己的解决方案来帮助他人. (下面的第一个答案)

I was stuck for a while on implementing a simple bootstrap modal dialog box and found pieces of the answer in about 10 different pages. Considering I couldn't find the answer quickly nor clearly I thought I'd share my solution to help others. (first answer below)

如果您必须添加多种类型的引导窗口小部件,我建议您看一下( https://ng-bootstrap.github.io/#/home )

In case you have to add multiple types of bootstrap widgets I suggest taking a look at (https://ng-bootstrap.github.io/#/home)

推荐答案

src/index.html 中,我将body标签的内容更改为:

In the src/index.html I changed the content of the body tag to:

 <body>
    <app-root></app-root>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> 
    </script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"> 
    </script>
</body>

在调用模态的组件中,我在模板中:

In the component, which calls the modal, I have in the template:

<!-- Button to Open the Modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal" (click)="showModal()">
  Open modal
</button>
<app-modal></app-modal>

在打字稿组件中

    showModal(): void {   
        this.displayService.setShowModal(true); 
        // communication to show the modal, I use a behaviour subject from a service layer here
    }

我在模版中为模态构建了一个单独的组件

I build a separate component for the modal, in the template I have

<!-- The Modal -->
<div class="modal fade" 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" (click)="hideModal()">&times;</button>
      </div>

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

      <!-- Modal footer -->
      <div class="modal-footer">
        <button type="button" class="btn btn-primary" (click)="sendModal()" >Send</button>
        <button type="button" class="btn btn-danger" (click)="hideModal()">Close</button>

        <!-- this button is hidden, used to close from typescript -->
        <button type="button" id="close-modal" data-dismiss="modal" style="display: none">Close</button>
      </div>
    </div>
  </div>
</div>

在Typescript组件中,我有

And in the Typescript component I have

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

    // This lets me use jquery
    declare var $: any;

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

      ngOnInit() {
      }
      showModal():void {
        $("#myModal").modal('show');
      }
      sendModal(): void {
        //do something here
        this.hideModal();
      }
      hideModal():void {
        document.getElementById('close-modal').click();
      }
    }

现在,模态对话框可以工作了,它具有一个发送功能(可以在其中添加一些其他逻辑)和一个隐藏功能,用于从打字稿中关闭模态

Now the modal dialog works, has a send function where some additional logic can be, and a hide function to close the modal from typescript

这篇关于在angular7中实现引导模式对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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