我尝试使用HandleBars循环使用以下JSON;
{
"IndexValues": {
"inv": {
"1": {
"live": {
"INV": "1",
"ITP": 719.166962
},
"day": 14,
"week": 85,
"month": 232
},
"2": {
"live": {
"INV": "2",
"ITP": 719.166962
},
"day": 14,
"week": 93,
"month": 237
},
"dayK": 28,
"week": 178,
"montk": 469
}
}
}
上面的JSON我尝试使用以下HandleBar循环:
{{#data.IndexValues.inv}}
<!-- the following is showing data, but is not dynamic -->
{{this.1.live.INV}}
<br>
{{this.2.live.INV}}
<!-- the following doenst work -->
{{#this}}
{{this.live.INV}}
{{/this}}
<!-- the following is showing data, but is not dynamic -->
{{#this}}
{{this.1.live.INV}}
{{/this}}
{{/data.IndexValues.inv}}
问题是“inv”是动态的,可能在1-256范围内
答案 0 :(得分:0)
Handlebars循环仅适用于数组元素([]表示法),而不适用于对象字段({}表示法)。您的示例JSON中没有数组。您可以(a)更改您的JSON以获得列表,或者(b)创建一个新数组,然后将其传递给您的Handlebars模板。
(a)更改JSON(添加“inv.data”列表)
{
"IndexValues": {
"inv": {
data: [{
"id" : 1,
"live": {
"INV": "1",
"ITP": 719.166962
},
"day": 14,
"week": 85,
"month": 232
},
{
"id" : 2,
"live": {
"INV": "2",
"ITP": 719.166962
},
"day": 14,
"week": 93,
"month": 237
}],
"dayK": 28,
"week": 178,
"montk": 469
}
}
}
你的Handlebar模板将是:
{{#data.IndexValues.inv.data}}
...
{{/#data.IndexValues.inv.data}}