问题描述
为了使这个例子尽可能简单,假设我在home.html中有以下代码:
To make this example as simple as possible, let's say I have the following code in home.html:
<html>
<head>
<!-- ALL DEPENDENCIES FOR ICANHAZ ARE INCLUDED ABOVE -->
<script type="text/html" id="foo" src="js_template.js"></script>
<script>ich.foo({})</script>
</head>
<body></body>
</html>
在javascript_template.js中,我有以下内容:
And in javascript_template.js, I have the following:
Hello world!
事实证明,icanhaz没有检测到foo,所以ich.foo({})正在投掷一个错误。究竟是怎么回事?
As it turns out, icanhaz is not detecting foo, so ich.foo({}) is throwing an error. What exactly is going on here?
推荐答案
ICanHaz.js不会自动下载 src 。这种行为可以在ICH.js的源代码的第510行看到,其中它在定义模板之前检查脚本标记的innerHTML属性。
ICanHaz.js does not automatically download the contents of src
. This behavior can be seen on line 510 of ICH.js's source code, in which it checks for an innerHTML property of the script tag before defining the template.
您必须定义它内联,或使用您自己的AJAX请求。例如,embedded:
You must define it inline, or use your own AJAX request. For example, embedded:
<script type="text/html" id="foo">
Hello, world
</script>
或者,如果您使用的是jQuery,则可以使用AJAX加载脚本:
Or, if you are using jQuery, you can use AJAX to load the script:
$(function(){
$.get('js_template.js', function(res){
ich.addTemplate('foo', res);
});
});
请注意, ich.foo()
在AJAX请求完成之前将无法使用。
Keep in mind, ich.foo()
will not be available until the AJAX request completes.
这篇关于icanhaz找不到模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!