引用外部模板腳本當我使用jQuery指腳本...jQuery的通過ID
var template = _.template($('#homeTemplate').html(), args);
...它的工作原理,但只有當腳本在<head>
這樣的...
<script type="text/template" id="homeTemplate">
<div class="homePage">
<% /* stuff */ %>
</div>
</script>
如果我將它移動到這樣一個外部文件...
<script src="js/templates/hometemplate.js" type="text/template" id="homeTemplate"></script>
它停止工作。爲什麼?我該如何解決這個問題?
非Ajax解決方案是什麼? – PrimeLens
在hometemplate.js中,必須添加模板字符串作爲全局變量...類似於:'window。_tempTemplate ='您的模板';'然後,在您的主腳本中包含引用global var:'_.template(window._tempTemplate);' – adamb
瞭解它。只要將這個獎勵作爲我正確接受的答案。當我列出可能的選擇時,我也想呼籲Dennis Cheung的評論。謝謝阿丹姆 – PrimeLens