-1
<div data-bind="foreach : list1">
<div data-bind="foreach : Land">
<div data-bind="text : Description">
</div>
</div>
</div>
var ViewModel = function() {
var self = this;
var _json =[
{
"Land": {
"Description": "BBA-Reman",
"ID": 1,
"CreateDate": null,
"CreatedBy": null,
"UpdateDate": null,
"UpdatedBy": null
},
"Zoning": {
"Description": "VA Zoning",
"ID": 1,
"CreateDate": null,
"CreatedBy": null,
"UpdateDate": null,
"UpdatedBy": null
},
"Insurence": {
"CompanyName": "Geico",
"Address": null,
"Phone": null,
"ID": 1,
"CreateDate": null,
"CreatedBy": null,
"UpdateDate": null,
"UpdatedBy": null
}
},
{
"Land": {
"Description": "Star Mind",
"ID": 1,
"CreateDate": null,
"CreatedBy": null,
"UpdateDate": null,
"UpdatedBy": null
},
"Zoning": {
"Description": "VA Zoning",
"ID": 1,
"CreateDate": null,
"CreatedBy": null,
"UpdateDate": null,
"UpdatedBy": null
},
"Insurence": {
"CompanyName": "Geico",
"Address": null,
"Phone": null,
"ID": 1,
"CreateDate": null,
"CreatedBy": null,
"UpdateDate": null,
"UpdatedBy": null
}
}
]
self.list1 = ko.observableArray(_json);
}
ko.applyBindings(new ViewModel());
为什么我的嵌套foreach不起作用?嵌套的foreach用法在knockout js中不起作用
我喜欢这样,那么它的工作
<div data-bind="foreach : list1">
<div data-bind="with: Land">
<div data-bind="text : Description">
</div>
</div>
</div>
这里结合使用的那一刻是我的jsfiddle http://jsfiddle.net/tridip/kxumu4rs/2/
'Land'包含一个对象而不是一个数组,所以你不能使用'foreach',但是你需要使用'with',因为你已经实现了。但是使用'with'有什么问题,你想达到什么目的? – nemesv
没有问题使用'with',但我想知道,如果我需要使用嵌套的'foreach'然后我需要在代码改变?你可以指出吗?谢谢 – Mou
对于使用嵌套'foreach',你需要嵌套数组。目前,你只需要在你的JSON一个大阵中还有没有其他的阵列中有:'Land'和'Zoning'和'Insurence'都是对象,而不是数组。 – nemesv