Javascript ReferenceError:未使用下划线模板定义变量

Javascript ReferenceError:未使用下划线模板定义变量,javascript,jquery,underscore.js,Javascript,Jquery,Underscore.js,我正在使用jQuery、jQueryMobile和下划线模板制作一个应用程序,但我从来没有遇到过这个错误,我找不到发生在模板上的变量: self.$list = $("#list"); var template = _.template('<%= foo %>', {foo: 'hello'}); self.$list.html(template); self.$list=$(“#list”); var-template=551;.template(“”,{

我正在使用jQuery、jQueryMobile和下划线模板制作一个应用程序,但我从来没有遇到过这个错误,我找不到发生在模板上的变量:

    self.$list = $("#list");
    var template = _.template('<%= foo %>', {foo: 'hello'});
    self.$list.html(template);
self.$list=$(“#list”);
var-template=551;.template(“”,{foo:'hello'});
self.$list.html(模板);
错误:ReferenceError:未定义foo

这是一个例子,如果我试图呈现一个类别列表,我也会有同样的感觉

    _.each(categories, function(category){
        console.log(category); //<-- ok
        var template = _.template(self.$categoryTemplate.text(), category);
        self.$list.append(template);
    });
\每个(类别、功能(类别){

console.log(category);//调用
\uu.template
返回一个函数,该函数将带有模板变量的对象作为第一个参数

所以你应该这样做:

self.$list = $("#list");
var template = _.template('<%= foo %>')({foo: 'hello'});
self.$list.html(template);
self.$list=$(“#list”);
var-template=551;.template(“”)({foo:'hello'});
self.$list.html(模板);
这是:

_.each(categories, function(category){
    console.log(category); //<-- ok
    var template = _.template(self.$categoryTemplate.text())(category);
    self.$list.append(template);
});
\每个(类别、功能(类别){

console.log(category);//非常感谢,它可以工作,但我不明白为什么上面的操作不起作用。@oscar:the
.template
界面最近发生了变化,有关详细信息,请参阅我对副本的回答。
_.each(categories, function(category){
    console.log(category); //<-- ok
    var template = _.template(self.$categoryTemplate.text())(category);
    self.$list.append(template);
});