【问题标题】:Angular 2 app doesnt load JS scripts in htmlAngular 2 应用程序不会在 html 中加载 JS 脚本
【发布时间】:2017-03-31 00:34:42
【问题描述】:

我的 Angular 2 应用程序的 html 代码中有一些脚本,但它们没有加载。当我检查浏览器的控制台时,我什至看不到加载脚本的尝试?我如何以 Angular 2 加载 JS 脚本

div class="col-lg-4 col-md-4 col-sm-6 col-xs-12">
                    <div class="alert alert-dismissible fade in" role="alert">
                        <script type="text/javascript">
                              setInterval(function() {
                                $('#element').load('/Resources/speedtest.txt');
                            }, 1000);
                        </script>
                        <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                        <span aria-hidden="true">×</span></button>
                        <span id="element">Loading...</span>
                    </div>
                </div>

其中一个脚本有一个示例。它在一个名为 home 的组件中使用,我使用它所在的模板 home.html。

这里是 home 组件代码。

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

@Component({
    selector: 'home',
    templateUrl: './home.html'
})
export class homecomponent { }

【问题讨论】:

    标签: javascript jquery html angularjs angular


    【解决方案1】:

    Angular 只是从模板中去除&lt;script&gt; 标签。您需要其他方式来加载脚本。例如在你的组件代码中使用 requirejs。

    另见https://github.com/angular/angular/pull/12172

    【讨论】:

    • 我有一种感觉是需要的,所以我基本上需要将js脚本放在组件的导出区域中?也非常感谢!
    • 这可能会有所帮助stackoverflow.com/questions/38103920/…我自己还没用过。
    • ahhhh 好的,谢谢,这些信息对我有很大帮助,我可以从这里找到答案!非常感谢您的帮助,非常感谢!
    猜你喜欢
    • 2017-05-29
    • 2016-10-21
    • 2020-05-13
    • 1970-01-01
    • 1970-01-01
    • 2019-03-26
    • 2017-03-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多