【问题标题】:Dynamically load external javascript file not working in angular 6动态加载外部javascript文件在角度6中不起作用
【发布时间】:2021-08-12 15:37:53
【问题描述】:

我正在使用Angular 6,正在尝试在我的项目中添加Bootstrap Multiselect。但是这里遇到了一些问题bootstrap-multiselect.js 没有加载到页面中。

所以我想动态添加这个bootstrap-multiselect.js 文件,但它没有加载请帮我解决这个问题

index.html

<!doctype html>
<html>
   <head>
      <meta charset="utf-8">
      <title></title>
      <base href="/">
      <meta name="viewport" content="width=device-width, initial-scale=1">
   </head>
   <body>
      <my-app>loading</my-app>
      <!-- Inclusion of sgwt widgets -->
      <noscript>Please enable JavaScript to continue using this application.</noscript>
   </body>
   <!-- Multiselect code -->
   <link rel="stylesheet" href="http://davidstutz.github.io/bootstrap-multiselect/dist/css/bootstrap-multiselect.css" type="text/css" />
   <script type="text/javascript" src="http://davidstutz.github.io/bootstrap-multiselect/docs/js/jquery-2.2.4.min.js"></script>
   <script type="text/javascript" src="http://davidstutz.github.io/bootstrap-multiselect/docs/js/bootstrap.bundle-4.5.2.min.js"></script>
   <script type="text/javascript" src="http://davidstutz.github.io/bootstrap-multiselect/docs/js/prettify.min.js"></script>
   <script type="text/javascript">
      $(document).ready(function() {
            $('#example-getting-started').multiselect({
              includeSelectAllOption: false,
              enableFiltering: true,
              includeFilterClearBtn: false,
              enableCaseInsensitiveFiltering : true,
            });
        });
   </script>
   <!-- Multiselect code -->
</html>

app.component.ts

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

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  ngOnInit() { 
     this.loadScript('http://davidstutz.github.io/bootstrap-multiselect/dist/js/bootstrap-multiselect.js');
   }

   public loadScript(url: string) {
    const body = <HTMLDivElement> document.body;
    const script = document.createElement('script');
    script.innerHTML = '';
    script.src = url;
    script.async = false;
    script.defer = true;
    body.appendChild(script);
  }
}

app.component.html

<!-- Build your select: -->
<select id="example-getting-started" multiple="multiple">
    <option value="cheese">Cheese</option>
    <option value="tomatoes">Tomatoes</option>
    <option value="mozarella">Mozzarella</option>
    <option value="mushrooms">Mushrooms</option>
    <option value="pepperoni">Pepperoni</option>
    <option value="onions">Onions</option>
</select>

还是这样显示的:

请在angular 6 中帮助解决这个问题。这个问题可能已经问过了,但这并不能解决我的问题。

提前致谢。

【问题讨论】:

    标签: jquery angular bootstrap-4 multi-select angular-lifecycle-hooks


    【解决方案1】:

    我已经像你一样从那个网站导入了库,但只更改了 jquery。

      <link rel="stylesheet" href="https://davidstutz.github.io/bootstrap-multiselect/docs/css/bootstrap-4.5.2.min.css"
        type="text/css">
      <link rel="stylesheet" href="https://davidstutz.github.io/bootstrap-multiselect/docs/css/bootstrap-example.min.css"
        type="text/css">
      <link rel="stylesheet" href="https://davidstutz.github.io/bootstrap-multiselect/docs/css/prettify.min.css"
        type="text/css">
      <link rel="stylesheet"
        href="https://davidstutz.github.io/bootstrap-multiselect/docs/css/fontawesome-5.15.1-web/all.css" type="text/css">
    
    
      <script src="https://code.jquery.com/jquery-2.2.4.min.js"
        integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
      <script type="text/javascript"
        src="https://davidstutz.github.io/bootstrap-multiselect/docs/js/bootstrap.bundle-4.5.2.min.js"></script>
      <script type="text/javascript" src="https://davidstutz.github.io/bootstrap-multiselect/docs/js/prettify.min.js">
      </script>
    
      <link rel="stylesheet" href="https://davidstutz.github.io/bootstrap-multiselect/dist/css/bootstrap-multiselect.css" type="text/css">
      <script src="https://davidstutz.github.io/bootstrap-multiselect/dist/js/bootstrap-multiselect.js"></script>
    

    这里是the stackblitz

    在 ngb-modal 内部:stackblitz

    【讨论】:

    • 感谢您的回答,我将实施您的代码
    • 告诉我进展如何
    • 您好,我正面临error typeerror $(...).multiselect is not a function
    • 我的文件夹结构是这样的ibb.co/x3rQ4Pt 我在我的子组件中添加了你的ngAfterViewInit() 代码请帮我解决这个问题
    • 您好,我没有包含类型定义文件,但在我的 app.component.ts 中添加了 declare var $: any; 以抑制类型错误。你试过吗?
    猜你喜欢
    • 1970-01-01
    • 2020-10-15
    • 1970-01-01
    • 2017-10-27
    • 1970-01-01
    • 2021-03-04
    • 1970-01-01
    • 2011-01-07
    • 2021-08-06
    相关资源
    最近更新 更多