2015-10-21 60 views
1

我有2個html文件(a.html和index.html)和1個javascript文件(file.js)在a.html -------我有一個不同語言的下拉菜單(如英語,德國,印地文)。從下拉菜單中選擇1種語言。如何使用angularJS動態地將腳本添加到index.html?

<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 ---基於從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); 
} 

在file.js中,如果我做了console.log(document)...它表明腳本標記附加在index.html的主體中。但是,當我選擇語言時,結果並未反映出來。

用於附加到的index.html

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

index.html的腳本標籤----- 如果我手動附加的腳本標籤index.html中示出的結果,但是當我動態更新它。腳本標記會附加到index.html文件,但結果不會反映出來。我如何重新加載index.html,使動態添加的內容在運行應用程序時也反映出來?

回答

0

嘗試通過使用setAttribute(),而不是直接賦值,更改:

scr.src = search_quarry; 

scr.setAttribute('src', search_quarry); 
+0

它不工作 – hmims

+0

進行改變,在運行時的index.html(我檢查這個由console.log(文件);)但沒有反映。 – hmims

+0

您可以發佈小提琴或完整的代碼示例嗎?您可以將其刪除不相關的數據 – Meir

相關問題