我怎样才能让有条件的knockout.js约束力? [英] How can I make conditional binding in knockout.js?
本文介绍了我怎样才能让有条件的knockout.js约束力?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想这一个:
<div data-bind="attr: {class: itemSelected? 'selected' : 'unselected' }"></div>
但它不工作:)
But it does not work :)
推荐答案
假设你有这样的:
function viewModel() {
this.itemSelected = ko.observable(true);
}
ko.applyBindings(new viewModel());
添加()后itemSelected拿到观察到的当前值,您可以用三元运算符是:
Add a () after itemSelected to get the current value of the observable that you can use with the ternary operator:
&LT; DIV数据绑定=ATTR:{:'选择'itemSelected():上课吗?未选中的'}&GT;&LT; / DIV&GT;
如果您并不需要指定用于非选定状态未被选中的类,你可以这样做,而不是:
If you didn't need to assign the unselected class for the non selected state you could do this instead:
<div data-bind="css: { selected: itemSelected }"></div>
这篇关于我怎样才能让有条件的knockout.js约束力?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文