剔除“如果结合".不工作 [英] knockout "if binding" not working
问题描述
使用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屋!