使用客户端模板的脚本标签,为什么不能通过src属性加载?
问题描述:
这正常工作(可以通过id访问dom中的代码段)
this works fine (am able to access the snippet in the dom by id)
<script type="text/x-template" id="todo-item-template">
<div class="todo-view">
{blah} {blah}
</div>
</script>
但是如果我将模板放在外部资源中,则无法在dom中找到它:
but if i put the template in an external resource, i can't find it in the dom:
<script type="text/x-template" id="todo-item-template" src="todo-item-template.html">
chrome给我一个警告,我认为这是无关紧要的:
chrome is giving me a warning which i think is irrelevant:
Resource interpreted as Script but transferred with MIME type text/html:
"http://localhost:8000/todo2/todo-stats-template.html".
我正在从文件系统和/或 python -m SimpleHTTPServer
运行.有什么想法吗?
i am running from the filesystem, and/or from python -m SimpleHTTPServer
. any ideas?
答
这是因为服务器将 .html
文件作为HTML文件提供.您将希望模板文件具有不同的文件类型,例如 .tpl
,并让服务器发送适当的标头.
This is because the server is serving .html
files as HTML files. You will want to have template files be a different file type, such as .tpl
and have the server send the appropriate headers.