如何在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 sttatement不工作的任何建议?我是否正确地比较了这两个条件?
答案 0 :(得分:6)
假设id
是一个可观察的,你需要将它作为一个函数调用,而不是将它作为一个属性。请尝试以下方法:
{{if $item.current_cmt_id == id()}}
答案 1 :(得分:0)
你需要包围&lt;%= comment.id%&gt;报价?
<ul data-bind='template: { name: "attachmentListTemplate", foreach: attachmentsModel.convAttachments, templateOptions: {current_cmt_id: "<%=comment.id%>"} }'> </ul>