简体   繁体   English

将带有document.write的外部JavaScript文件加载到AngularJS应用程序中

[英]Load external JavaScript file with document.write into AngularJS app

I got this external JavaScript file(Ad file), which has a document.write() in it. 我得到了这个外部JavaScript文件(广告文件),里面有一个document.write()。 I have tried loading it with $http, and then injecting it into a document.createElement, but the Ad server doesn't support this method; 我尝试用$ http加载它,然后将其注入document.createElement,但广告服务器不支持此方法; $http.get. $ http.get。

I have tried ng-include in a script, but no work. 我在脚本中尝试过ng-include,但没有工作。

I need to load the script in the file, and the run the script. 我需要在文件中加载脚本,然后运行脚本。 How do I do it? 我该怎么做?

<script ng-src=""></script> doesn't work either. <script ng-src=""></script>也不起作用。 It does not run the script. 它不运行脚本。

Thanks. 谢谢。

如果你正在使用jQuery(它听起来像你),有一个方法

$.getScript("http://my/script/url.js");

I ended up with this directive - helped made by my friend Dennis - that works in my situation. 我最终得到了这条指令 - 由我的朋友丹尼斯帮助制作 - 在我的情况下有效。 Hope it can help someone. 希望它可以帮助某人。

(function() {
    'use strict';

    angular
        .module('app')
        .directive('emediateScript', emediateScript);

    /* @ngInject */
    function emediateScript(Ad, $http) {
        var directive = {
            link: link,
            restrict: 'E',
            scope: {
                ad: '=ad',
            }
        };

        return directive;

        function link(scope, element, attrs){

            var _el = angular.element(element);

            var url = 'http://www.http.com';

            var request = {
                method: 'GET',
                url: url,
                headers: {
                    'X-Authentication': undefined
                }
            };

            if (url) {
                $http(request).then(function(response) {
                    var html = response.data.substr(16);
                    html = html.substring(0, html.length - 4);
                    _el.html(html);
                });
            }

        };
    };
})();

And HTML was; HTML就是;

<emediate-script></emediate-script>

Why not simply create a <script> tag with jqLite? 为什么不简单地用jqLit​​e创建<script>标签?

$('body').append('<script type="application/javascript" src="http://localhost/test.js"></script>');

It seems to work perfectly with my test file. 它似乎与我的测试文件完美配合。

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

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