在模板中使用If条件

AnA*_*ice 2 jquery jquery-templates knockout.js

你如何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>
Run Code Online (Sandbox Code Playgroud)

其中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>
Run Code Online (Sandbox Code Playgroud)

有关为什么if sttatement无效的任何建议?我正确地比较了这两个条件吗?

Luk*_*ett 6

假设这id是一个可观察的,你需要将它作为一个函数调用,而不是将它作为一个属性.请尝试以下方法:

{{if $item.current_cmt_id == id()}}
Run Code Online (Sandbox Code Playgroud)