KnockoutJS css 绑定 != true
我的视图模型返回 true 值,并且我正在尝试让我的模板相应地添加 CSS。问题是,我找不到 a != true 的语法。
我有这样的事情:
<div data-bind="css: {'lw-touched': checked, 'lw-touch': !checked}"></div>
我想会说,如果选中=== true,则应用“lw-touched”,如果选中=== false,则应用“lw-touch”。但这不起作用:(。所以我尝试了这个:
<div data-bind="css: {'lw-touched': checked, 'lw-touch': checked !== true}"></div>
这也不起作用。
我确信有办法做到这一点!我只是目前找不到它。
My view model is returning a true value, and I'm trying to get my template to add CSS accordingly. The problem is, I can't find the syntax for a != true.
I have something like this:
<div data-bind="css: {'lw-touched': checked, 'lw-touch': !checked}"></div>
Which I thought would say, apply 'lw-touched' if checked === true, apply 'lw-touch' if checked === false. But that doesn't work :(. So I tried this:
<div data-bind="css: {'lw-touched': checked, 'lw-touch': checked !== true}"></div>
Which also didn't work.
I'm sure there is a way to do this! I just can't find it at the moment.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我在 30 秒后发布并解决了它:(。
我离开它是因为其他人可能也有同样的问题。
而且,感谢 @MikaelÖstberg,我使用了更好的语法,
我将其标记为答案,这样我就不会得到更多负面反馈:/
I posted and solved it 30 seconds later :(.
I'm leaving it because someone else might have this same problem.
Also I used the better syntax thanks to @MikaelÖstberg
I'm marking this as the answer so that I don't get more negative feedback :/
考虑在绑定字段添加 ()。
Consider to add the () at the binding field.
谢谢g.breeze的回答。我一直在尝试在 css 属性上使用三元运算符,但没有成功,但从来不知道可以在没有花括号的情况下设置它。一本用于书籍!
您用宝贵的答案触及了一个两年前的话题。
Thank you g.breeze, for your answer. I've been unsuccessfully trying to use the ternary operator on the css property, but never knew it could be set without the curly braces. One for the books!
You've bumped a two-year-old topic with your valuable answer.