【问题标题】:Bootstrap 4.0 "Modal" not working with Angular 4Bootstrap 4.0“模态”不适用于 Angular 4
【发布时间】:2018-01-12 18:29:10
【问题描述】:

无法让 Bootstrap 4.0 modal 在 Angular 4 项目中工作。

模态在一个简单的静态 HTML 页面中效果很好:https://jsfiddle.net/Lq73nfkx/1/

<!-- MODAL -->
<!-- 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">Save changes</button>
      </div>
    </div>
  </div>
</div>

您可以在下面的屏幕截图中看到,按下显示 Modal 会导致 html 在多个位置发生更改,不仅在模型 HTML 本身内,而且还对文档的 &lt;body&gt; 元素进行了操作。

执行 HTML 操作的 Bootstrap JavaScript 似乎没有在 Angular 4 项目中触发,当我在项目中使用完全相同的代码时,HTML 根本没有改变。

还有其他方法可以让我将 Bootstrap 与 Angular 4 一起使用吗?或者我应该使用另一个不是 Bootstrap 的 Angular 4 模态解决方案?

【问题讨论】:

标签: angular bootstrap-4


【解决方案1】:

您可以使用ngx-bootstrap 库来执行此操作。安装库后,请按照以下步骤操作。

Step1:将以下代码添加到app.module.ts文件中。

import { ModalModule, BsModalRef } from 'ngx-bootstrap';

@NgModule({
 imports: [ModalModule.forRoot(),...],
 providers: [BsModalRef]
})

Step2:复制以下代码并粘贴到app.commponent.html。

    <button type="button" class="btn btn-primary" (click)="openModal(template)">Create template modal</button>

<ng-template #template>
    <div class="modal-header">
        <h4 class="modal-title pull-left">Modal</h4>
        <button type="button" class="close pull-right" aria-label="Close" (click)="modalRef.hide()">
            <span aria-hidden="true">&times;</span>
        </button>
    </div>
    <div class="modal-body">
        This is a modal.
    </div>
</ng-template>

Step3:最后将以下代码复制到app.component.ts。

import { Component, TemplateRef } from '@angular/core';
import { BsModalService } from 'ngx-bootstrap/modal';
import { BsModalRef } from 'ngx-bootstrap/modal/bs-modal-ref.service';

export class AppComponent {
    modalRef: BsModalRef;
    constructor(private modalService: BsModalService) {}

    openModal(template: TemplateRef<any>) {
        this.modalRef = this.modalService.show(template,{ backdrop: 'static', keyboard: false });
    }
}

【讨论】:

    【解决方案2】:

    在导入引导 cdn 时,您可以尝试在 jQuery 之后添加 TetherJS

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
    <script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-18
      • 2019-09-25
      • 1970-01-01
      • 1970-01-01
      • 2018-06-10
      • 2019-12-09
      • 2021-08-16
      • 1970-01-01
      相关资源
      最近更新 更多