【问题标题】:Meteor Handlebars: value in array in {{#each}} blockMeteor Handlebars:{{#each}} 块中数组中的值
【发布时间】:2016-08-01 20:31:14
【问题描述】:

我是 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}}

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

【问题讨论】:

    标签: javascript meteor handlebars.js


    【解决方案1】:

    您当前正在比较两个数组,而不是两个标量。 $eq 不会工作。由于您正在遍历计划,因此您实际上想查看当前(标量)计划 this 是否在当前用户拥有的计划的 array 中。

    {{#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;
    });
    

    【讨论】:

      猜你喜欢
      • 2013-12-30
      • 2023-03-21
      • 2016-03-21
      • 2014-04-17
      • 2016-06-01
      • 1970-01-01
      • 2015-09-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多