是否可以使用值绑定将 Knockout 可观察属性绑定到单选按钮?
这是我正在尝试做的事情,但值最终是字符串“[Object object]”,而不是我的可观察属性的实际实例:
<input type="radio" name="vehicleGroup" data-bind="checked: vehicleGroupViewModel().selectedGroupOption , value:vehicleGroupViewModel().car" />
<input type="radio" name="vehicleGroup" data-bind="checked: vehicleGroupViewModel().selectedGroupOption , value:vehicleGroupViewModel().truck" />
这是我正在使用的视图模型:
var VehicleGroupViewModel = function(){
var self = this;
this.selectedVehicleGroup = ko.observable();
this.selectedGroupOption = ko.observable();
this.selectedGroupOption.subscribe(function (newVal) {
self.selectedVehicleGroup(newVal);
}
this.selectedGroup = ko.observable();
this.car = ko.observable(new VehicleViewModel());
this.truck = ko.observable(new VehicleViewModel());
}
var VehicleViewModel = function(){
this.name = ko.observable();
}
所以最后我希望 Car 或 Truck VehicleViewModel 位于 selectedVehicleGroup 可观察对象中。