我将把所有HTML模板保存到一个文件中。此文件包含大量模板(超过50个)。
<script type="text/html" id="template-1">
<p>Template 1</p>
</script>
<script type="text/html" id="template-2">
<p>Template 2</p>
</script>我需要将模板加载到js变量。
我看过JQuery load方法,但是它加载模板来将它附加到DOM,这不是我需要的。
任何JQuery或AngularJS解决方案都适用于我。
发布于 2015-06-10 13:35:43
您可以使用$.get进行常规的ajax调用,然后从响应中取出元素(而无需向DOM附加任何内容)。
例如
$.get("templateUrl", function (data) {
var $doc = $(data);
var firstTemplate= $doc.filter('#template-1').html()
var secondTemplate e= $doc.filter('#template-2').html()
//..Load rest of templates into variables
});https://stackoverflow.com/questions/30757835
复制相似问题