Javascript Knockout.js-ObservableArray不返回值
在击出模式下,该功能可以正常工作并绑定:Javascript Knockout.js-ObservableArray不返回值,javascript,knockout.js,Javascript,Knockout.js,在击出模式下,该功能可以正常工作并绑定: 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.curr
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]);
我不明白这为什么不起作用。此模式无法在我的应用程序的其他部分正常工作。要访问阵列,必须将其展开:
self.currentCustomer = ko.observable(self.customers()[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());
谢谢你,我就知道这一定很简单。