2012-06-18 157 views
1

我有麻煩呈現出一個模板字段,我的數據是這樣的:JSRender留下空白

var data = {"addresses": 
       [{"Title":null, 
        "Name":null, 
        "Address1":"Add 1.1", 
        "Address2":"Add = 2.1", 
        "Town":"Town1", 
        "County":"County 1", 
        "Country":"United Kingdom", 
        "Postcode":"CV10 1RH", 
        "CountryId":229}, 
       {"Title":null, 
        "Name":null, 
        "Address1":"Add 1.2", 
        "Address2":"Add = 2.2", 
        "Town":"Town2", 
        "County":"Count 2", 
        "Country":"United Kingdom", 
        "Postcode":"CV10 2RH", 
        "CountryId":229} 
       ]}; 

我的模板存儲在javascript和呈現:

var defaultAdressSelectorTemplate = '<label for="addressSelector">Select Adress:</label><select id="addressSelector" name="addressSelector">{{#each addresses}}<option>{{>Address1}}</option>{{#/each}}</select>';   

$.templates({ addressTemplate: defaultAdressSelectorTemplate }); 

html = $.render.addressTemplate(data); 

我遇到的問題是{{>Address1}}總是呈現空白。我究竟做錯了什麼?

回答

1

使用要使用{{for}}

var defaultAdressSelectorTemplate = '<label for="addressSelector">Select Adress:</label><select id="addressSelector" name="addressSelector">{{for addresses}}<option>{{>Address1}}</option>{{/for}}</select>'; 

jsfiddle example

0

嘗試使用jQuery.parseJSON()方法之前呈現模板

var renderedTemplate = $("#templateId").render($.parseJSON(data)); 
$("#dataGridId").html(renderedTemplate); 
最新JSRender