最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Knockout data-bind if else condition - Stack Overflow

programmeradmin3浏览0评论

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

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.

Thanks

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

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.

Thanks

Share Improve this question asked Nov 1, 2013 at 11:51 MujahidMujahid 1,2375 gold badges32 silver badges64 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

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:

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'.

发布评论

评论列表(0)

  1. 暂无评论