我很困惑是什么导致了这里的问题,因为这个绑定处理程序通常工作正常(下)
ko.bindingHandlers.buttonGroupChecked = {
init: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
var value = valueAccessor();
var newValueAccessor = function() {
return {
click: function() {
value(allBindingsAccessor.get('val'));
}
};
};
ko.bindingHandlers.event.init(element, newValueAccessor,
allBindingsAccessor, viewModel, bindingContext);
},
update: function(element, valueAccessor, allBindingsAccessor,
viewModel, bindingContext) {
if (allBindingsAccessor.get("val") === ko.unwrap(valueAccessor())) {
helpers.activeClassSingle(element, "btn-info", "btn-success");
}
}
};
我在第 7 行不断收到错误,value(allBindingsAccessor.get('val'));
说值不是函数?
这些选项在我的视图模型中定义如下: -
self.yesNoOptions = [
{val: 1, text: "Yes"},
{val: 0, text: "No"}
];
相应的 HTML 和绑定是:-
<div class="btn-group btn-group-justified" data-bind="foreach: $root.yesNoOptions">
<div class="btn btn-lg btn-info" data-bind="buttonGroupChecked: $root.currentVariation().variationAgreed, val: val, text: text"></div>
</div>
Where$root.currentVariation().variationAgreed
是当前选择的项目,并且是作为以下对象的一部分的可观察对象。
var observableWorkItemVariation = function(data){
var self = this;
data = data || {};
self.id = ko.observable(data.id || "");
self.orderWorkItemID = ko.observable(data.orderWorkItemID || "");
self.variationAgreed = ko.observable(data.variationAgreed || 0);
self.changeWorkBillable = ko.observable(data.changeWorkBillable || 1);
self.declareBillable = ko.observable(data.declareBillable || 0);
self.changeWorkBillable.subscribe(function(val){
if(self.changeWorkBillable() == 0){
self.declareBillable(0);
}
});
self.changeWorkPayable = ko.observable(data.changeWorkPayable || 1);
self.variationCode = ko.observable(data.variationCode || "");
}
它正确突出显示所选项目(否,因为它默认为 0)但是当我尝试更改它时会引发错误。