2015-01-02 19 views
4

我得到了這個外部JavaScript文件(Ad文件),其中包含一個document.write()。我曾嘗試使用$ http加載它,然後將其注入到document.createElement中,但廣告服務器不支持此方法; $ http.get。將帶有document.write的外部JavaScript文件加載到AngularJS應用程序中

我已經嘗試ng-include在腳本中,但沒有工作。

我需要在文件中加載腳本,然後運行腳本。我該怎麼做?

<script ng-src=""></script>也不起作用。它不運行腳本。

謝謝。

+0

您是否試圖在運行時注入腳本? –

+0

是:) @RahilWazir – Tommy

回答

1

我結束了這個指令 - 在工程 - 幫我的朋友丹尼斯我的情況。希望它可以幫助某人。

(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); 
       }); 
      } 

     }; 
    }; 
})(); 

而且HTML是;

<emediate-script></emediate-script> 
0

爲什麼不簡單地用jqLit​​e創建一個<script>標籤?

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

它似乎與我的測試文件完美地工作。

+0

它創建

  • 11. 將外部數據加載到SproutCore應用程序
  • 12. Android將外部本地JavaScript文件加載到WebView中
  • 13. document.write加載外部Javascript源時不起作用
  • 14. 將外部TPL文件加載到JST
  • 15. Cordova Windows 10通用應用程序 - 如何加載外部JavaScript文件?
  • 16. 使用Javascript加載外部文件
  • 17. 在外部JavaScript文件中加載jquery
  • 18. 在Vue.js中加載外部JavaScript文件
  • 19. 如何將動態外部組件加載到Angular應用程序中
  • 20. Codeigniter從外部應用程序文件夾加載視圖?
  • 21. 從應用程序外部將文件拖放到QTableView中
  • 22. 如何將外部的php或html文件加載到我的iPhone應用程序的某個部分中
  • 23. 帶有AngularJS的JavaScript中的Windows Phone應用程序
  • 24. 將文件url從Winforms控件拖到外部應用程序
  • 25. 在SPA應用程序中使用AngularJs在ng-view中加載外部頁面
  • 26. 加載外部JavaScript文件後的onload()
  • 27. 刷新外部加載的JavaScript文件
  • 28. 如何將angularJS模塊加載到應用程序中?
  • 29. 使用django將數據加載到外部文本文件中
  • 30. 加載文件到Java應用程序