2017-07-31 39 views
0

说我有以下数据#each内parent属性:把手 - #如果基于

[ 
    { 
    id: 1, 
    someFoo: true, 
    things: [ 
     { blah: "bang" }, 
     { blah: "bing" } 
    ] 
    }, 
    { 
    id: 2, 
    someFoo: false, 
    things: [ 
     { blah: "boop" }, 
     { blah: "barp" } 
    ] 
    } 
] 

在车把,我怎么能检查是否someFoo{{#each things}}内是真的吗?

我已经试过:

{{#each things}} 
<p>{{blah}}</p> 
{{#if ../someFoo}}<p>Yay!</p>{{/if}} 
{{/each}} 

没有运气。

编辑,对不起,它看起来像我的阵列有点误导。实际使用案例是我有一个发票可能包括税。因此,数据是这样的:

{ 
    tax: 0, // <-- this could be non-0, in which case should be "truthy" 
    orders: [ 
     {words: 2, text: "some text", pay: 5.4}, 
     {words: 3, text: "some more text", pay: 7.8} 
    ] 
} 

和模板是这样的:

<table class="clear"> 
    <thead> 
    <tr> 
     <th>Description</th> 
     <th>Quantity</th> 
     {{#if tax}}<th>PU HT</th>{{else}}<th>PU</th>{{/if}} 
     {{#if tax}}<th>TVA</th>{{/if}} 
     {{#if tax}}<th>Total HT</th>{{else}}<th>Total</th>{{/if}} 
    </tr> 
    </thead> 
    <tbody> 
    {{#each orders}} 
    <tr> 
     <td>Start of text: {{substring text}}...</td> 
     <td align="right">1.00 U</td> 
     <td align="right">{{accounting pay}}</td> 
     {{#if ../tax}}<td align="right">20.00%</td>{{/if}} 
     <td align="right">{{accounting pay}}</td> 
    </tr> 
    {{/each}} 
    </tbody> 
</table> 

我的问题是{{#each}}作品外{{#if}},而不是里面的一个。

回答

2

没有什么错在你的代码段中,除其他循环数据。

{{#each this}} 
    {{#each things}} 
    <p>{{blah}}</p> 
    {{#if ../someFoo}}<p>Yay!</p>{{/if}} 
    {{/each}} 
{{/each}} 

您访问的变量使用../

+0

啊,我可能会把你带走我的一系列物品。我想传达的是模板只对一个对象起作用,但对于每个对象,“someFoo”可能是真或假。 无论如何,看起来'../'在{{#if}}块中不起作用 – alt

+0

argh!我以为你和我都不会有这种错误 - 事实证明,我的应用程序没有刷新模板,所以最终确实发挥了作用。 – alt

0

这是我会怎么写,你会发现你someFoo财产与这个,因为它是指每个东西

{{#each things}} 
<p>{{blah}}</p> 
{{#if this.someFoo}}<p>Yay!</p>{{/if}} 
{{/each}} 
+0

嗯,我不知道这是正确的当前每个块的1度最高,为'someFoo'不'things'存在 - 这是在'事''parent – alt