简体   繁体   中英

Uncaught TypeError: Cannot call method 'replace' of null

If I type "_.template($('#pranks-list').html())" on Chrome JS console it's works as well

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

app.js // Views

window.PranksListView = Backbone.View.extend({

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

Index.html

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

  </body>

Why I get this error on this line??

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

It's hard to tell without seeing the whole code, but you are probably trying to run _.template($('#pranks-list').html()) before the dom is created and the node is there. Usually its a good practice to render the template on render time when you have the template variables ready:

_.template($('#pranks-list').html(), {id: 'foo', name: 'bar'});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM