5

如果我在 Chrome JS 控制台上键入“_.template($('#pranks-list').html())”,它也可以工作

>> _.template($('#pranks-list').html())
function (a){return e.call(this,a,b)}

app.js // 视图

window.PranksListView = Backbone.View.extend({

    template: _.template($('#pranks-list').html())
});

索引.html

  <script type="text/html" id="pranks-list">
    <li><a href='#pranks/<%= id %>'><%= name %></a></li>
  </script>

  </body>

为什么我在这条线上得到这个错误?

template: _.template($('#pranks-list').html())
4

1 回答 1

15

没有看到整个代码就很难判断,但是您可能正在尝试_.template($('#pranks-list').html())在创建 dom 并且节点存在之前运行。通常,当您准备好模板变量时,在渲染时渲染模板是一个好习惯:

_.template($('#pranks-list').html(), {id: 'foo', name: 'bar'});
于 2012-05-18T20:57:50.537 回答