2017-01-04 28 views
1
有車把

我環路添加類元素與手把#each通過與給定的布爾屬性像OBJETS:基於財產

elements: [ 
    { 
    text: "a" 
    active: true 
    }, 
    { 
    text: "b" 
    active: false 
    } 
] 

什麼是一類添加到基於HTML元素的最簡單方法那財產?事情是這樣的:

{{#each elements}} 
    <article class={{if active 'active-class'}}> 
    ... 
{{/each}} 

我使用車把4.0.6

回答

5

您需要通過打開一個helper block開始:

{{#if}} 

然後,您可以添加您檢查的參數反對(在這種情況下是一個布爾值):

{{#if active}} 

然後,關閉它像這樣:

{{#if active}}{{/if}} 

然後,你想,如果滿足條件呈現內容進去之間的花括號:

{{#if active}}text{{/if}}> 

所以,你的HTML看起來是這樣的:

{{#each elements}} 
    <article {{#if active}}class="active"{{/if}}> 
    ... 
{{/each}} 

或者,如果您還想要該元素內的靜態類,您可以這樣做:

{{#each elements}} 
    <article class="example {{#if active}}active{{/if}}"> 
    ... 
{{/each}}