我试图理解 Handlebars 模板,但我不明白为什么我的模板没有更新? jquery.js
和 handlebars-v2.0.0.js.js
已成功加载。控制台没有任何输出。
<body>
<ul class="tweets"></ul>
<script id="entry-template" type="text/x-handlebars-template">
<li>
<p><h2>{{author}}</h2></p>
<p>{{tweet}}</p>
</li>
</script>
<script type="text/javascript" src="handlebars-v2.0.0.js"></script>
<script type="text/javascript" src="js/jquery.js"></script>
<script>
(function () {
var data = {
author: 'Alex apple',
tweet: '30 Days to learn jQuery'
};
var source = $('#entry-template').html();
var template = Handlebars.compile(source);
var temp = template(data);
console.log(temp);
})();
</script>
</body>
最佳答案
您似乎忘记了实际调用匿名函数:
(function(){
// the rest of the code skipped
})(); // note the parens
就目前而言,您只需定义一个函数,但不执行它。请注意,该 block 应放置在 jquery 和 Handlebars 加载器之后 - 否则 $
和 Handlebars
都将是未定义的。
关于jquery - 模板未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27584605/