【问题标题】:Using an If condition inside a template在模板中使用 If 条件
【发布时间】:2011-07-03 04:38:56
【问题描述】:

如何在 jQuery 模板中使用 IF 语句?

这是我的模板:

<script type="text/html" id="attachmentListTemplate">
{{if $item.current_cmt_id == id }}
    <li>
        <a href="${link}" class="fancyIFrame clearfix">${title}</a>
    </li>
{{/if}}
</script>

其中 id 本质上是 ${id} 并且由数据绑定传递(通过 KnockoutJS)。没有 IF 语句的输出很好,如下所示:${$item.current_cmt_id}

这是数据绑定(由 KnockoutJS 提供支持):

<ul data-bind='template: { name: "attachmentListTemplate", foreach: attachmentsModel.convAttachments, templateOptions: {current_cmt_id: <%=comment.id%>} }'> </ul>

关于为什么 if 语句不起作用的任何建议?我是否正确比较了这两种情况?

【问题讨论】:

  • 您也使用服务器端语言吗?如果是这样,源是否显示整数? (因为 我问...)
  • 是的,它会在浏览器中呈现为 INT,111

标签: jquery knockout.js jquery-templates


【解决方案1】:

假设id 是一个可观察对象,您需要将其作为函数调用,而不是将其视为属性。请尝试以下操作:

{{if $item.current_cmt_id == id()}}

【讨论】:

  • 做到了!你是我的英雄 - 谢谢!
  • 好电话,这可能很难追踪!
【解决方案2】:

您需要用引号将 括起来吗?

        <ul data-bind='template: { name: "attachmentListTemplate", foreach: attachmentsModel.convAttachments, templateOptions: {current_cmt_id: "<%=comment.id%>"} }'> </ul>

【讨论】:

  • 不,这很好用。 jQuery模板的东西,我不知道如何做条件
  • 我看到一些模板系统使用 作为占位符,所以我想知道是不是也是这样。猜不到。
猜你喜欢
  • 2016-08-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-24
  • 2012-07-07
  • 1970-01-01
  • 2012-08-18
  • 1970-01-01
相关资源
最近更新 更多