// Model
function User(item) {
var self = this;
this.id = ko.observable(item.id);
this.name = ko.observable(item.name);
this.surname = ko.observable(item.surname);
this.email = ko.observable(item.email);
// item.enabled is a boolean value.
this.enabled = ko.observable(item.enabled);
this.credit = ko.observable(new Credit(item.credit));
this.roles = ko.observableArray(item.roles);
}
<div data-bind='foreach: users'>
<!-- This below prints 1 -->
<span class='badge ' data-bind='text: enabled' ></span>
<!-- Both of those below don't activate (if check fails) -->
<span class='badge badge-success' data-bind='if: enabled==1'>OK</span>
<span class='badge ' data-bind='if: enabled==0' >NO</span>
</div>
我试过了:
<span class='badge badge-success' data-bind='if: enabled==1'>OK</span>
<span class='badge badge-success' data-bind='if: enabled'>OK</span>
<span class='badge badge-success' data-bind='if: enabled()'>OK</span>
<span class='badge badge-success' data-bind='if: enabled=="1"'>OK</span>
没有运气。我做错了什么?
答案 0 :(得分:1)
你很接近,但正确的语法是
<span class='badge badge-success' data-bind='if: enabled() == 1'>OK</span>
<span class='badge ' data-bind='if: enabled() == 0' >NO</span>
演示JSFiddle。
因为enabled
是一个可观察的(它是一个函数),所以当你在表达式中使用enabled()
时,你需要通过调用它来获取它的值而不需要任何参数:{{1}}。