<分区>
我正在使用 jQuery、jQueryMobile 和下划线模板制作一个应用程序,但我从来没有遇到过这个错误,我找不到发生在模板上的变量:
self.$list = $("#list");
var template = _.template('<%= foo %>', {foo: 'hello'});
self.$list.html(template);
错误:ReferenceError:foo 未定义
这是一个例子,如果我尝试渲染一个类别列表,我会有同样的感觉。
_.each(categories, function(category){
console.log(category); //<-- ok
var template = _.template(self.$categoryTemplate.text(), category);
self.$list.append(template);
});
类别模板:
<script id="category-template" type="text/template">
<li><a href="#"><% = name %></a></li>
</script>
我不明白为什么你看不到模板中的变量