好的,所以我已经广泛使用 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")}'>