【发布时间】:2017-09-21 00:23:30
【问题描述】:
好的,所以我已经广泛使用 KO 样式绑定来显示用户输入数据的内联验证,并且效果很好。这是我的绑定之一:
data-bind='style: {backgroundPosition: ! currentRegistry().title() || !$root.currentRegistry().clientLogin() ? "top" : "bottom"}
我遇到的麻烦是我想做这样的绑定,除了在第二个样式绑定上,如果 if 语句的计算结果为 false,我希望它返回到由设置的任何背景位置之前的绑定,而不是仅仅回到“底部”:
data-bind='style: {backgroundPosition: ! currentRegistry().title() || !$root.currentRegistry().clientLogin() ? "top" : "bottom"}, style: {backgroundPosition: currentRegistry().title() && $root.currentRegistry().clientLogin() && ! $root.hasRegImage() ? "20px" : "bottom"}'
换句话说,对于第二个绑定,我想说的是 style: {backgroundPosition: if stuff ? "THIS"} 否则不改变样式。希望这是有道理的,任何使这种情况发生的提示或指示将不胜感激!谢谢。
编辑:
虽然 RodneyTrotter 的解决方案无疑是获得相同功能的更优雅的方法,但我确实从一位同事那里学到了一种方法,该方法完全符合我的要求。我没有意识到你可以让每个属性都有条件,又名:
data-bind='style: {backgroundPosition: ! $root.currentRegistry().clientLogin() || ! $root.currentRegistry().title() ? "top" : (! $root.hasRegImage() ? "20px" : "bottom")}'>
【问题讨论】:
标签: binding knockout.js styles