2

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

这是我的模板:

<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 语句不起作用的任何建议?我是否正确比较了这两种情况?

4

2 回答 2

6

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

{{if $item.current_cmt_id == id()}}
于 2011-02-24T00:25:04.360 回答
0

您需要用引号将 <%=comment.id%> 括起来吗?

        <ul data-bind='template: { name: "attachmentListTemplate", foreach: attachmentsModel.convAttachments, templateOptions: {current_cmt_id: "<%=comment.id%>"} }'> </ul>
于 2011-02-24T00:15:14.983 回答