2012-11-29 105 views
1

在淘汰賽中,這個工程並正確綁定:Knockout.js - ObservableArray沒有返回值

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

但是,下面的沒有。我不明白爲什麼這是行不通的。這種模式在我的應用程序的其他部分正常工作。

回答

3

訪問數組,你必須解開它:

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

謝謝,我知道它必須簡單。 –

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());