[英]Knockout data-bind if else condition
In my current project, I have a knockout binding where the layout height should be applied according to the value received as true or false. 在我当前的项目中,我有一个剔除绑定,其中应该根据收到的值为true或false来应用布局高度。 Following is my binding code
以下是我的绑定代码
data-bind="style: {height: showOld ? '392px' : '275px'}"
The showOld
gives either true
or false
correctly, but, regardless what it returns, it always take 392px
. showOld
正确给出了true
或false
,但是,无论返回什么,它始终占用392px
。 If the showOld
gives true
then 392px
should return else 275px
should return. 如果
showOld
给true
然后392px
应该返回其他275px
应该返回。 Any help to fix this issue is highly appreciate. 非常感谢您提供任何解决此问题的帮助。
Thanks 谢谢
If your showOld
is ko.observable
then you need to write showOld()
(because ko.observable
is a function) to get its value in your expression: 如果您的
showOld
是ko.observable
则需要编写showOld()
(因为ko.observable
是一个函数)才能在表达式中获取其值:
data-bind="style: {height: showOld() ? '392px' : '275px'}"
From the documentation : 从文档中 :
To read the observable's current value, just call the observable with no parameters.
要读取可观察对象的当前值,只需调用不带参数的可观察对象。
To write a new value to the observable, call the observable and pass the new value as a parameter.
要将新值写入可观察对象,请调用可观察对象并将新值作为参数传递。 For example, calling
myViewModel.personName('Mary')
will change the name value to 'Mary'.例如,调用
myViewModel.personName('Mary')
会将名称值更改为'Mary'。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.