Meteor Handlebars:{{#each}}块中数组中的值

Meteor Handlebars: value in array in {{#each}} block

本文关键字:数组 Handlebars #each Meteor      更新时间:2024-05-04

我是使用Meteor包的Meteor/Handlebars的新手https://atmospherejs.com/aldeed/plans-stripe其中包含这两个助手。

AppPlans.listDefined() - to get the list of all our plans 
 - ["free", "monthly", "yearly"]
AppPlans.get() - to see which plan the user currently has. 
 - ["free"]

而且https://github.com/raix/Meteor-handlebar-helpers它有一个有用的助手:

{{$eq a b}} if a equals b then return true

我在HTML中显示计划,如下所示:

{{#each AppPlans.listDefined}}
   <strong>{{this}}</strong>
{{/each}}

但我想做的是这样的事情;不起作用

{{#each AppPlans.listDefined}}
   {{#if $eq AppPlans.listDefined AppPlans.get}}
        <strong>{{this}}</strong>
   {{else}}
      <span>{{this}}</span>
   {{/if}}
{{/each}}

解决这个问题的最佳方法是什么?

您当前正在比较两个数组,而不是两个标量。$eq不起作用。由于您在计划上循环,因此您实际上希望查看当前(标量)计划this是否在当前用户拥有的计划数组中。

{{#each AppPlans.listDefined}}
   {{#if $inArray this AppPlans.get}}
        <strong>{{this}}</strong>
   {{else}}
      <span>{{this}}</span>
   {{/if}}
{{/each}}

然后是$inArray助手:

Template.registerHelper('$inArray',function(s,a){
  return a.indexOf(s) > -1;
});