【问题标题】:Meteor Spacebars: {{#if}} inside {{#each}} produces unexpected results流星空格键:{{#each}} 内的 {{#if}} 会产生意想不到的结果
【发布时间】:2014-12-19 08:43:48
【问题描述】:

{{#each}} 中的 {{#if}} 的这个简单示例产生了一个意想不到的(对我来说)结果:

HTML:

<head>
    <title>test</title>
</head>

<body>
    {{> test yes=true}}
</body>

template name="test">
    {{#if yes}}
        <span>yes</span>
    {{else}}
        <span>no</span>
    {{/if}}
    <ul>
        {{#each testItems}}
            {{#if yes}}
                <li>yes</li>
            {{else}}
                <li>no</li>
            {{/if}}
        {{/each}}
    </ul>
</template>

JS:

Template.test.helpers({
    testItems: [1,2,3]
});

输出:

是的

  • 没有
  • 没有
  • 没有

我期待一个包含 3 x 是的列表...

这段代码有什么问题?

【问题讨论】:

    标签: meteor spacebars


    【解决方案1】:

    每个帮助程序中的数据上下文是 testItems 数组,但您正在尝试访问父上下文的变量(测试模板的数据上下文)。所以自然是未定义,从而导致 if 语句评估为假。如果您访问父上下文,您应该会得到预期的结果。

    <head>
        <title>test</title>
    </head>
    
    <body>
        {{> test yes=true}}
    </body>
    
    template name="test">
        {{#if yes}}
            <span>yes</span>
        {{else}}
            <span>no</span>
        {{/if}}
        <ul>
            {{#each testItems}}
                {{#if ../yes}}
                    <li>yes</li>
                {{else}}
                    <li>no</li>
                {{/if}}
            {{/each}}
        </ul>
    </template>
    

    【讨论】:

      猜你喜欢
      • 2019-04-05
      • 1970-01-01
      • 2016-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-18
      • 1970-01-01
      相关资源
      最近更新 更多