gpt4 book ai didi

javascript - if else 条件 knockout 数据绑定(bind)

转载 作者:行者123 更新时间:2023-11-28 15:53:30 25 4
gpt4 key购买 nike

在我当前的项目中,我有一个 knockout 绑定(bind),其中布局高度应根据收到的 true 或 false 值来应用。以下是我的绑定(bind)代码

data-bind="style: {height: showOld ? '392px' : '275px'}"

showOld 正确给出 truefalse,但是,无论它返回什么,它总是需要 392px >。如果 showOld 给出 true,则应返回 392px,否则应返回 275px。非常感谢任何解决此问题的帮助。

谢谢

最佳答案

如果您的 showOldko.observable 那么您需要编写 showOld() (因为 ko.observable > 是一个函数)以获取其在表达式中的值:

data-bind="style: {height: showOld() ? '392px' : '275px'}"

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

关于javascript - if else 条件 knockout 数据绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19726214/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com