您如何在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语句为什么不起作用的任何建议?我可以正确比较这两个条件吗?
最佳答案
假定id
是可观察的,则需要将其作为函数调用而不是将其视为属性。请尝试以下操作:
{{if $item.current_cmt_id == id()}}