简体   繁体   English

如何使用angularJS将脚本动态添加到index.html?

[英]How to dynamically add the script to index.html using angularJS?

I have 2 html files(a.html and index.html) and 1 javascript file(file.js) In a.html-------i have a drop-down menu of different languages(like english,german,hindi). 我有2个html文件(a.html和index.html)和1个javascript文件(file.js)。在a.html -------我有一个不同语言的下拉菜单(例如英语,德语,印地语)。 From the drop down 1 of the language is selected. 从下拉菜单中选择一种语言。

 <select ng-model="lang" class="form-control input-custom input-sm" ng-change="language(lang)" required>
        <option value="" selected>--Choose language--</option>
        <option value="en-us">english</option>
        <option value="id-id">indonesia</option>
 </select>

file.js---Based on the language selected from a.html file, i update the index.html file. file.js ---基于从a.html文件中选择的语言,我更新index.html文件。

$scope.language=function(lang){
  var search_quarry = "bower_components/angular-i18n/angular-locale_" + lang + ".js";
  var scr = document.createElement("script");
  scr.src = search_quarry;
  document.getElementsByTagName("body")[0].appendChild(scr);
  console.log(document);
}

In file.js if i do console.log(document)...it shows that the script tag is attached in the body of index.html. 在file.js中,如果我执行console.log(document)...,则表明脚本标记附加在index.html的正文中。 But the result is not reflected when i select the language. 但是,当我选择语言时,结果不会得到反映。

script tag for appending to index.html 用于附加到index.html的脚本标记

<script src="bower_components/angular-i18n/angular-locale_id-id.js"></script>

index.html----- If i append the script tag manually in index.html the result is shown but when i dynamically updates it..the script tag gets attached to index.html file but the results are not reflected. index.html -----如果我在index.html中手动添加了脚本标签,则会显示结果,但是当我动态更新它时.. script标签会附加到index.html文件中,但结果不会反映出来。 How can i reload index.html such that the dynamically added content also reflects while running the application? 如何重新加载index.html,以便动态添加的内容在运行应用程序时也能反映出来?

Try using setAttribute() instead of direct assignment, change: 尝试使用setAttribute()代替直接分配,更改:

scr.src = search_quarry;

to

scr.setAttribute('src', search_quarry);

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

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