对于这段代码
function Order(name, initialMeal) {
var self = this;
self.name = name;
self.meal = ko.observable(initialMeal);
}
function OrdersViewModel() {
var self = this;
self.availableMeals = [
{ menuItem:"Chicken", calories:1000, price:12 },
{ menuItem:"Eggs", calories:900, price:10 },
{ menuItem:"Fries",calories:700, price:15 }
];
self.orders = ko.observableArray([
new Order("Mike", self.availableMeals[0]),
new Order("John", self.availableMeals[0]),
new Order("Larry", self.availableMeals[0])
]);
self.addOrder = function(){
self.orders.push(new Order("", self.availableMeals[0]));
}
}
ko.applyBindings(new OrdersViewModel());
我有以下观点
<tbody data-bind="foreach: orders">
<tr>
<td><input data-bind="value: name" /></td>
<td><select data-bind="options: $root.availableMeals, value: meal, optionsValue: 'price', optionsText: 'menuItem'"></select></td>
<td data-bind="text: meal().price"></td>
</tr>
</tbody>
您可能可以在淘汰赛示例中找到它。问题是最后一个绑定餐()。当 optionsValue 存在于它之上时,价格似乎不起作用。如果我删除 optionsValue 它工作得很好。我错过了什么吗?谢谢。