javascript - Bootstrap 4 Modal 不适用于 Angular 2

标签 javascript html twitter-bootstrap angular

我正在尝试使用 https://v4-alpha.getbootstrap.com/components/modal/ 中的一个示例,向我的导航栏添加一个模式,但点击时没有任何反应。

<div class="pos-f-t fixed-top header">
    <nav class="navbar navbar-inverse bg-inverse navbar-toggleable-md">
        <button class="navbar-toggler navbar-toggler-right" (click)="toggleSidebar()">
            <span class="navbar-toggler-icon"></span>
        </button>
        <a class="navbar-brand" href="javascript:void(0)">Calculadora ISDB-Tb</a>
 <!-- Small modal -->
        <button type="button" class="btn btn-primary" data-toggle="modal" data-target=".bd-example-modal-sm">Small modal</button>

        <div class="modal fade bd-example-modal-sm" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
        <div class="modal-dialog modal-sm">
            <div class="modal-content">
              ...
            </div>
        </div>
        </div>
    </nav> 
</div>

最佳答案

我强烈建议你使用 angular this用于使用带 Angular Bootstrap ,因为它具有 Angular 所需的组件且无需 jquery 干预。

话虽如此,对于您的问题,您应该使用

在您的项目中安装 jquery
npm install jquery --save

之后,您必须在脚本部分下的项目 angular-cli 中包含 jquery

"scripts": [  
          "../node_modules/jquery/dist/jquery.min.js",
//other scripts
      ]

在此之后,在你的组件中你必须声明 $ 以使用 jquery 函数作为

declare var $:any;

然后在您的模板中为您的模式提供一个 id

 <div class="modal fade bd-example-modal-sm" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true" id="myModal">

点击按钮时调用函数

<button type="button" class="btn btn-primary" data-toggle="modal" data-target=".bd-example-modal-sm" (click)="openModal()">Small modal</button>

然后在您的组件中将函数定义为

openModal(){
$('#myModal').modal('show');
}

希望这对您有所帮助。如果出现任何问题,请告诉我

关于javascript - Bootstrap 4 Modal 不适用于 Angular 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45992642/

相关文章:

javascript - 仅定位带有文本的最后一级元素

javascript - 如何按日期对 javascript 数组进行排序

html - HTML/CSS滚动问题

javascript - 在angular bootstrap ui中点击关闭下拉菜单

html - 如何使折叠的 Bootstrap 列居中

javascript - 如何在 HTML 页面上隐藏/显示图像

javascript - 如何在 hello world 前 5 秒显示事件指示器

javascript - 更改按钮单击 codeigniter 中的枚举值

javascript - 为 :checked 之前的所有元素设置样式

twitter-bootstrap - Bootstrap 3 弹出窗口和工具提示位于同一元素上 - 工具提示不触发