javascript 敲除数据绑定 if else 条件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19726214/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Knockout data-bind if else condition
提问by Mujahid
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. Following is my binding code
在我当前的项目中,我有一个淘汰赛绑定,其中应根据收到的值为 true 或 false 应用布局高度。以下是我的绑定代码
data-bind="style: {height: showOld ? '392px' : '275px'}"
The showOld
gives either true
or false
correctly, but, regardless what it returns, it always take 392px
. If the showOld
gives true
then 392px
should return else 275px
should return. Any help to fix this issue is highly appreciate.
在showOld
给出任何true
或false
正确的,但是,不管它返回什么,它总是需要392px
。如果showOld
给出true
那么392px
应该返回否则275px
应该返回。非常感谢解决此问题的任何帮助。
Thanks
谢谢
回答by nemesv
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 readthe observable's current value, just call the observable with no parameters.
To writea 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'.
要读取observable 的当前值,只需调用不带参数的 observable。
要将新值写入observable,请调用 observable 并将新值作为参数传递。例如,调用
myViewModel.personName('Mary')
会将名称值更改为“Mary”。