javascript - 在 Angular html View 中加载外部脚本

标签 javascript angularjs

我已经注册了 Polldaddy 的付费版本,并获得了这个脚本标签(减去正确的 ID)。

<script type="text/javascript" charset="utf-8" src="http://static.polldaddy.com/p/0000000.js"></script>

在我的 View 中,此脚本必须加载到一个 div 中 - 但它不起作用。 Angular 在 View 中加载脚本有问题吗?是否可以?如果是这样,你能帮我理解怎么做吗?

如果您需要更多信息,请告诉我。

谢谢

最佳答案

由于 Angular script,您无法在 Angular 应用程序中加载脚本指令,因此您需要创建自己的指令。 像这样:

function polldaddy() {
    var injectScript = function(element) {
        var scriptTag = angular.element(document.createElement('script'));
        scriptTag.attr('charset', 'utf-8');
        scriptTag.attr('src', 'http://static.polldaddy.com/p/0000000.js');
        element.append(scriptTag);
    };

    return {
        link: function(scope, element) {
            injectScript(element);
        }
    };
}

angular
    .module('myApp')
    .directive('polldaddy', polldaddy);

然后在您的 HTML 中:

<div polldaddy></div>

关于javascript - 在 Angular html View 中加载外部脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30102299/

相关文章:

javascript - 如何使用jquery通过滑动效果从左侧删除旧div并从右侧添加新div?

javascript - 如果请求的窗口已存在于 Firefox 中,则调用 focus()

javascript - 有没有办法在 $watchGroup | 中只触发一次回调函数? Angular 型?

angularjs - 有没有办法向组件根元素添加一个类?

javascript - 当与缩放到包含标记一起使用时,ng-map 默认缩放过于放大 ="true"

javascript - 为什么 Angular 在使用 Rails Assets 管道包含它时不起作用?

javascript - 如果不使用 jQuery,如何使用 CSS 删除在我的文章中找到的这些特定标签 "<div><p>&nbsp;</p></div>"

javascript - (function instance).prototype 字段默认指向哪里?

javascript - 退格键在数字输入字段中不起作用 KaiOS |斯维尔特3

javascript - 通过 JS 读取 HTML 中 SVG 元素的属性