简体   繁体   English

如何在 Angular 4 应用程序中使用 Bootstrap 3 Modal?

[英]How to use Bootstrap 3 Modal in Angular 4 Application?

I tried every possible solution given here and I still have this error.我尝试了这里给出的所有可能的解决方案,但仍然出现此错误。 I'm using Bootstrap 3 and Angular 4. It happens when I try to show a modal using this:我正在使用 Bootstrap 3 和 Angular 4。当我尝试使用这个显示模态时会发生这种情况:

$("#myModal").modal('show');

It sounds like you haven't added the Bootstrap Javascript files to your Angular project in the correct way.听起来您没有以正确的方式将 Bootstrap Javascript 文件添加到您的 Angular 项目中。 What you should do is to add the files to your .angular-cli.json file.您应该做的是将文件添加到您的.angular-cli.json文件中。

"styles": [
  "styles.css",
  "../node_modules/bootstrap/dist/css/bootstrap.min.css"
],
"scripts": [
  "../node_modules/jquery/dist/jquery.min.js",
  "../node_modules/bootstrap/dist/js/bootstrap.min.js"
],

You can read more about it here . 您可以在此处阅读更多相关信息

Bonus Tips奖金提示

Instead of mixing jQuery with Angular, I recommend you to install a library such as ngx-bootstrap that have rewritten the Bootstrap components as Angular components instead, it makes it way easier to work with together with the rest of your Angular code.我建议您不要将 jQuery 与 Angular 混合使用,而是安装一个诸如ngx-bootstrap类的库,它已将 Bootstrap 组件重写为 Angular 组件,这样可以更轻松地与其余 Angular 代码一起使用。

Resources:资源:

Take a look @ this link .看看@这个链接 It has a working example and a question for the same with typings and without it.它有一个工作示例和一个关于打字和不打字的问题。 I have used bootstrap and jquery with Angular in that project, you can check the repo too.我在那个项目中使用了带有 Angular 的 bootstrap 和 jquery,你也可以检查 repo。

In your case you need to add the jquery files在您的情况下,您需要添加 jquery 文件

like this in your angular-cli.json像这样在你的angular-cli.json

  "styles": [
    "../node_modules/bootstrap-v4-dev/dist/css/bootstrap.min.css",
    "styles.css"
  ],
  "scripts": [
    "../node_modules/jquery/dist/jquery.js",
    "../node_modules/tether/dist/js/tether.min.js",
    "../node_modules/bootstrap-v4-dev/dist/js/bootstrap.min.js"

and then declare in component.然后在组件中声明。 declare var $: any;

This should work.这应该有效。

UPDATE更新

I went on to create a modal of bootstrap using just jquery in Angular using the steps i mentioned.我继续使用我提到的步骤在 Angular 中使用 jquery 创建一个引导程序模式。

It works Check this gh page link - LINK .它有效检查这个gh页面链接 - LINK

and if you want to check the source code for the same check LINK .如果您想检查相同检查LINK的源代码。

The answer i posted came from this link this is my page on Angular LINK我发布的答案来自此链接 这是我在 Angular LINK 上的页面

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM