【问题标题】:{{#if}} inside a {{#each}} with a parameter coming from the each item{{#if}} 在 {{#each}} 中,参数来自每个项目
【发布时间】:2015-09-09 23:19:05
【问题描述】:

我有一个 {{#each}},它本质上是遍历用户集合。我现在想检查用户是否是管理员来设置开关(实现)。简化版:

{{#each getUsers}}
    testing: {{_id}}  {{#if isAdmin "tTEScMyjCedano2oT"}}checked="checked"{{/if}}
{{/each}}

在这里,我在{{#if}} 的_id 参数中进行了硬编码,它按预期工作。 isAdmin 函数可以正常工作,{{#if}} 可以正常工作,{{_id}} 也可以正常工作(打印出正确的 id)。

当然,我不想硬编码传递给isAdmin 的id,而是希望它是{{_id}},但它不起作用。即

{{#each getUsers}}
    testing: {{_id}}  {{#if isAdmin "{{_id}}"}}checked="checked"{{/if}}
{{/each}}

不起作用,也不起作用

{{#each getUsers}}
    testing: {{_id}}  {{#if isAdmin "{{../_id}}"}}checked="checked"{{/if}}
{{/each}}

也没有

{{#each getUsers}}
    testing: {{_id}}  {{#if isAdmin {{id}}}}checked="checked"{{/if}}
{{/each}}

这样做的正确方法是什么?

【问题讨论】:

    标签: meteor spacebars


    【解决方案1】:

    不要使用双引号或花括号:

    {{#each getUsers}}
      testing: {{_id}}  {{#if isAdmin _id}}checked="checked"{{/if}}
    {{/each}}
    

    【讨论】:

    • 谢谢,它有效!我会接受的。这是因为它已经在{{}} 中,即来自#if 的那些吗?
    • 是的,目前您不能在助手中嵌套空格键子表达式。
    • 之所以起作用,是因为在您的#each 语句中,它将数据上下文更改为引用“每个”用户,因此,您可以简单地使用user._id 而不是user._id本身,因为它已经知道您指的是user
    猜你喜欢
    • 2016-08-12
    • 2012-11-18
    • 1970-01-01
    • 1970-01-01
    • 2015-07-31
    • 1970-01-01
    • 1970-01-01
    • 2011-11-28
    • 1970-01-01
    相关资源
    最近更新 更多