2015-04-29 28 views
2

在Backbone應用程序中使用下劃線時出現問題。在控制檯我未捕獲的SyntaxError:通過模板循環時在下劃線中出現意外的令牌')'

Uncaught SyntaxError: Unexpected token ')'

它引薦我到下劃線庫:

underscore.js:line 1443

我whant做的是選擇模板,通過ID

var UserList = Backbone.View.extend({ 
       el: '.page', 
       render: function(){ 
        var self = this; 
        var users = new Users(); 
        users.fetch({ 
         success:function(users){ 
          console.log(users); 
          var template = _.template($('#user_list_template').html(), users); 
          self.$el.html(template); 
         } 
        }); 
       } 
      }); 

這裏是我的腳本模板

<script type="text/template" id="user_list_template"> 

      <table class="table striped"> 
      <thead> 
       <tr> 
        <th>Name</th> 
        <th>Age</th> 
       </tr> 
      </thead> 
      <tbody> 
       <% _.each(users,function(user)){ %> 
        <tr> 
         <td><%= user.name %></td> 
         <td><%= user.age %></td> 
        </tr> 
       <% }); %> 
      </tbody> 
      </table> 

     </script> 

正如我發現,問題是在這一行:

var template = _.template($('#user_list_template').html(), users); 

請問你能幫我找到什麼問題嗎?

+0

可能是重複的http://stackoverflow.com/questions/25881041/backbone-js-template-example/25881231#25881231 – nikoshr

回答

5

<% _.each(users,function(user)){ %>此行在模板中的{之前有一個額外的)

+0

非常感謝!問題在於!) – volodymyr3131

相關問題