1. 先定义转义符,因为默认的转义符<%= %> 与jsp的冲突
_.templateSettings = {
interpolate : /\{\{(.+?)\}\}/g
};
2. 在页面编写一个模版
<script type="text/template" id="detailedBar-template">
<div class='title'>
<span class='label'>本次消费</span>
<span class='numOfDinner'>{{numOfDiner}}人用餐</span>
<span class='amount'><span class='symbol'>{{currencySymbol}}</span>{{amount}}</span>
</div>
</script>
3. 调用这个js模板
var detailedBartemplate = _.template($('#detailedBar-template').html(),
{numOfDiner: detailedInfo.numOfDiner, currencySymbol: self.currencySymbol, amount: detailedInfo.amount}
);
$detailedList.append(detailedBartemplate);
Thinking
调用时,_.template(,)的第二个参数位是指定传入模板的参数
注意代码的红色强调处
Uncaught TypeError: Cannot call method ‘replace’ of null (underscore.js:768)
You’re calling an underscore template put not passing in the data it uses in the template. You’re referencing an underscore template that doesn’t exist. You could have created a new template but forgot to change it’s id.