Knockout.js的“如果绑定"在多个布尔值上 [英] Knockout.js "if Binding" on multiple booleans

查看:85
本文介绍了Knockout.js的“如果绑定"在多个布尔值上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在多个布尔值上使用Knockout的 如果绑定 ?

Is it possible to use Knockout's if binding on more than one boolean?

例如

<div data-bind="if: (property.aTrueValue && property.anotherTrueValue)">...

我尝试了很多不同的语法,但是似乎找不到正确的语法. 我不确定是否有可能.

I've tried a lot of different syntax, but can't seem to find the right syntax. I'm not sure it's even possible.

推荐答案

当敲除处理您的绑定时,它将首先计算您的表达式.

When Knockout processes your bindings it first evaluates your expression.

如果该表达式产生一个可观察的结果,则它会对该可观察的结果进行评估,以方便获得if:起作用的最终值.

If the expression results in an observable, it then evaluates the observable as a convenience to get the final value that the if: works on.

因此,以下两个代码完全相同

So the two following work identically

<div data-bind="if: foo"></div>
<div data-bind="if: foo()"></div>

一旦您离开了以可观察结果结尾的简单表达式的世界,您可能还想抛弃一切,总是自己评估可观察对象(出于理智的考虑).

Once you leave the world of simple expressions ending in an observable, you probably also want to leave the sugar behind and always evaluate the observables yourself (for sanity if nothing else).

尝试以下

<div data-bind="if: (property.aTrueValue() && property.anotherTrueValue())">...

这篇关于Knockout.js的“如果绑定"在多个布尔值上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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