我有以下对象:
"hasDevices": {
"hasLaptops": { "title": "abc" }
"hasTablets": { "title": "abc" }
}
我正在迭代:
{{#each hasDevices }}
{{> hasDevicesPartial this}}
{{/each }}
这就是hasDevicesPartial的样子:
{{#if hasLaptops }}
{{#each hasLaptops as |hasLaptop| }}
{{> anotherPartial hasLaptop }}
{{/each }}
{{/if }}
{{#if hasTablets }}
{{#each hasTablets as |hasTablet| }}
{{> anotherPartial hasTablet }}
{{/each }}
{{/if }}
我遇到的问题是,当我遍历hasDevices时,我传递了当前对象但是hasDevicesPartial看不到属性hasLaptops或hasTablets除了里面的内容,即标题。我如何遍历hasDevices,以便我能够传递hasLaptops或hasTablets对象,以便hasDevicesPartial可以在if语句中访问它以检查它是否存在。
答案 0 :(得分:1)
你不应该在一个简单的对象上使用{{#each}},这是为数组设计的。如果你想要一个数组使用不同的结构。
{ "shops" : [
{ "Shop1" :
"hasDevices": {
"laptops": [ { "HP": "Spectre360" } , { "Microsoft" , "Surface book" } ]
}
},
{ "Shop2" :
"hasDevices": {
"laptops": [ { "Apple": "Macbook" } ],
"tablets": [ { "Apple": "Ipad" } ]
}
}
}
通过这样的结构,您可以遍历商店,然后遍历其他属性
{{#each shops}}
{{> shopsPartial this}}
{{/each }}
和你的部分
{{#if laptops }}
{{#each laptops as |hasLaptop| }}
{{> anotherPartial hasLaptop }}
{{/each }}
{{/if }}
{{#if tablets }}
{{#each tablets as |hasTablet| }}
{{> anotherPartial hasTablet }}
{{/each }}
{{/if }}