如果我在index.html中添加以下代码,那么它工作正常,但我想在外部文件中定义,然后我想用can.view
加载它。
<script type="text/mustache" id="todosList">
{{#todos}}
<li>{{description}}</li>
{{/todos}}
</script>
如果我创建单独的html文件,如何从中加载模板?
答案 0 :(得分:1)
can.view
可以直接从网址加载模板。 From the can.view documentation:
从网址
加载模板要从网址加载,只需将模板的位置传递给 can.view。模板的位置需要匹配的扩展名 模板的类型:
document.getElementById('recipes') .appendChild(can.view('templates/recipes.ejs', recipeData ) )
注意:如果您使用的是RequireJS,则URL将相对于其baseUrl。
在您的情况下,如果您将文件命名为todos.mustache
并且它位于templates
目录中,则它将如下所示:
document.getElementById('todos') .appendChild(can.view('todos.mustache', recipeData ) )
答案 1 :(得分:0)
$('target-element').html(can.view('todosList', todolist-data))
这是在canJS中加载外部模板的语法。