2017-07-31 75 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