剔除“如果结合".不工作 [英] knockout "if binding" not working

查看:76
本文介绍了剔除“如果结合".不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Chrome进行调试时,我可以看到CoverPrices具有9个元素. foreach循环实际上运行良好,并且表看起来正确,并且第一个跨度已正确绑定到Item1.

When debugging with Chrome, I can see CoverPrices has 9 elements. The foreach loop actually works well and the table looks correct with the first span being bound to Item1 correctly.

但是,if绑定不起作用,并且显示两个图像.但是,Item2中的所有元素都具有真实值,因此仅应显示第一张图像.

However, the if binding does not work and both images are displayed. Yet, all the elements in Item2 have the true value, so only the first image should show up.

<!-- ko foreach: CoverPrices -->
    <tr>
        <td>
            <span data-bind="text: Item1"></span>
        </td>
        <!-- ko foreach: Item2 -->
        <td>
            <img src="~/Images/yes.png" alt="oui" data-bind="if: $data" /> 
            <img src="~/Images/no.png" alt="non" data-bind="ifnot: $data" /> 
        </td>
        <!-- /ko -->
    </tr>
    <!-- /ko -->

我的装订有什么问题吗?

Is there something wrong with my binding ?

推荐答案

if-binding不会影响整个元素,但会影响其内容.而且,由于img元素没有内容,因此绑定无关紧要.

The if-binding does not affect the whole element, but its content. And because an img element does not have content, the binding does not matter.

这将与span作为容器元素一起工作:

This will work, with span as container elements:

<span data-bind="if: $data"><img src="~/Images/yes.png" alt="oui" /></span>
<span data-bind="ifnot: $data"><img src="~/Images/no.png" alt="non" /></span>

如果您不希望使用其他元素,还可以使用无容器语法:

There is also a container-less syntax, if you don't want the additional elements:

<!-- ko if: $data -->
    <img src="~/Images/yes.png" alt="oui" />
<!-- /ko -->
<!-- ko ifnot: $data -->
    <img src="~/Images/no.png" alt="non" />
<!-- /ko -->

这篇关于剔除“如果结合".不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆