2017-05-17 8 views
1

ハンドルバーを使用して反復値を比較したい。これは私のコードですハンドルバーを使用した反復値の比較

{{#each accounts}} 
    {{#each projects}} 
     {{#if}} (compare accounts.project_id with projects._id) 
      // display the project name 
     {{else}} 
      // display not found 
     {{/if}} 
    {{/each}} 
{{/each}} 

助けてください。私はハンドルバーが新です/

答えて

0

handlebars-helpersモジュールの{{compare}}ヘルパーを使用してください。

{{#each accounts}} 
    {{#each projects}} 
     {{#compare accounts.project_id "==" projects._id) 
      // display the project name 
     {{else}} 
      // display not found 
     {{/compare}} 
    {{/each}} 
{{/each}} 

ヘルパーのインストール方法と使用方法については、を参照してください。

0

あなたはとても似Handlebarsに簡単なhelpersでそうすることができる。

Handlebars.registerHelper('if_eq', function(a, b, opts) { 
    if(a == b) 
     return opts.fn(this); 
    else 
     return opts.inverse(this); 
}); 

とあなたのコードで

...

{{#each accounts}} 
    {{#each projects}} 
     {{#if_eq accounts.project_id projects._id}} 
      // display the project name 
     {{else}} 
      // display not found 
     {{/if_eq}} 
    {{/each}} 
{{/each}} 
関連する問題