0
下劃線模板看起來很簡單,但我正在努力嘗試第一次,並且難住了。下劃線模板變量 - 剛開始
我只是想在H1中顯示一個人的名字。
這裏是我的模板:
<script id="tmpl-index-list" type="text/template">
<% _.each(data, function(item){ %>
<li>
<a href="#person" data-transition="slide">
<h1><%=item.personName%></h1>
</a>
</li>
<% }); %>
這是我的JS:
var tmplMarkup = $('#tmpl-index-list').html();
var compiledTmpl = _.template(tmplMarkup, {data: IndexList.listEntries});
$('#index-list-wrapper').html(compiledTmpl);
IndexList.listEntires是一個ListEntry對象,簡單的鍵/值對象的數組。
例如,這裏是「類」:
function ListEntry(){
this.img_url = '';
this.personName = '';
this.personID = '';
};
當我做了console.dir就可以了,數據是存在的,但對於item.personName在模板中,它出現爲未定義。
我錯過了什麼?
在此先感謝。
查看你在控制檯中得到了什麼:'<%_.each(data,function(item){console.log(item)%>' –
'item'是一個數組,但是項目[0]出現未定義。 –
請發佈一些精確的「IndexList.listEntries」示例內容 – mVChr