更新时间:2023-11-30 22:49:52
假设你有这个:
function viewModel() {
this.itemSelected = ko.observable(true);
}
ko.applyBindings(new viewModel());
在 itemSelected 后加一个 () 来获取可以与三元运算符一起使用的 observable 的当前值:
Add a () after itemSelected to get the current value of the observable that you can use with the ternary operator:
<div data-bind="attr: { class: itemSelected() ? 'selected' : 'unselected' }"></div>
如果您不需要为非选择状态分配未选择的类,您可以改为这样做:
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>