1

我在渲染模板时遇到问题,我的数据如下所示:

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}}总是呈现为空。我究竟做错了什么?

4

2 回答 2

1

使用您要使用的最新 JSRender{{for}}

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

jsfiddle 示例

于 2012-06-18T12:08:44.250 回答
0

在渲染模板之前尝试使用 jQuery.parseJSON() 方法

var renderedTemplate = $("#templateId").render($.parseJSON(data));
$("#dataGridId").html(renderedTemplate);
于 2013-03-18T07:44:20.040 回答