1

在淘汰赛中,这可以正常工作并正确绑定:

self.currentCustomer = ko.observable(new Customer("a","b","c","d","e","f","g","h"));

但是,下面没有。

// Random list of customers
self.customers = ko.observableArray([
    new Customer("a","b","c","d","e","f","g","h")
]);


self.currentCustomer = ko.observable(self.customers[0]);

我无法弄清楚为什么这不起作用。这种模式在我的应用程序的其他部分中正常工作。

4

2 回答 2

3

要访问数组,您必须打开它:

self.currentCustomer = ko.observable(self.customers()[0]);
于 2012-11-29T01:46:25.150 回答
0

补充一点,视图如下:

<p>Customer Name <input data-bind="value: currentCustomer().name" /></p>

模型如下:

function AppViewModel() {
   // Random list of customers
self.customers = ko.observableArray([
    new Customer("ABC Corp.")
]);


self.currentCustomer = ko.observable(self.customers()[0]); // This is the correction.

}


function Customer(_name)
{
    this.name = ko.observable(_name);
}

// Activates knockout.js
ko.applyBindings(new AppViewModel());
于 2012-11-29T11:30:23.813 回答