我很难理解为什么这个绑定设置不起作用。
我有一个具有id和名称的Page对象,我有一个具有batchDocumentId observable和pages observableArray的pendingBatchDocument。在我的视图模型中,我尝试用PendingBatchDocument初始化一个可观察的数组,并用它们的页面数组初始化这些PendingBatchDocuments。
语法没有给我任何错误,所以我假设那里的设置是正常的。如果它不正确,请告诉我。
我的问题是,为什么第二个foreach上的绑定不起作用?
视图
<div data-bind="foreach: pendingDocs">
<ul class="sortable" data-bind="foreach: pendingDocs().pages()">
</ul>
</div>Javascript视图模型
function Page(id, name)
{
this.id = ko.observable(id);
this.name = ko.observable(name);
}
var PendingBatchDocument = function(batchDocumentId, pages)
{
this.batchDocumentId = ko.observable(batchDocumentId);
this.pages = ko.observableArray(pages);
};
var ViewModel = function()
{
this.list1 = ko.observableArray([
{ itemId: "C1", name: "Item C-1" },
{ itemId: "C2", name: "Item C-2"},
{ itemId: "C3", name: "Item C-3"},
{ itemId: "C4", name: "Item C-4"},
{ itemId: "C5", name: "Item C-5"},
{ itemId: "C6", name: "Item C-6"},
{ itemId: "C7", name: "Item C-7"}]);
this.pendingDocs = ko.observableArray([
new PendingBatchDocument(1, [
new Page(1, "Page 1"), new Page(2, "Page 2"), new Page(3, "Page 3")
])
]);
};
ko.applyBindings(new ViewModel());JSBin http://jsbin.com/ivavew/3/edit
发布于 2013-07-03 06:06:01
foreach绑定中的上下文是单个数组元素,这意味着在foreach: pendingDocs中,您已经可以访问PendingBatchDocument实例,因此可以直接使用它的pages属性:
<div data-bind="foreach: pendingDocs">
<ul class="sortable" data-bind="foreach: pages">
</ul>
</div>https://stackoverflow.com/questions/17436324
复制相似问题