这不起作用
<div data-bind="foreach:nonAdminIB">
<div><span data-bind="text: explanation"></span> </div>
</div>
这确实显示了数据,但我对索引号进行了硬编码,我真的不知道会有多少项。
<div><span data-bind="text: nonAdminIB[0].explanation"></span> </div>
<div><span data-bind="text: nonAdminIB[1].explanation"></span> </div>
以下是发送到浏览器的数据
"nonAdminIB": {
"0": {
"nonAdminIrregularBehaviorId": 383,
"irregularBehaviorId": 5,
"irregularBehaviorDescription": "Falsified Information",
"explanation": "Falsification of information on applications/scheduling permits."
},
"1": {
"nonAdminIrregularBehaviorId": 384,
"irregularBehaviorId": 6,
"irregularBehaviorDescription": "Falsified Score",
"explanation": "Falsification of score information. "
}
我如何让foreach工作?
答案 0 :(得分:2)
nonAdminIB
目前是一个对象,foreach
仅适用于数组。
你必须将它转换为数组:
var nonAdminIBArray = [];
for(var prop in nonAdminIB) {
if(nonAdminIB.hasOwnProperty(prop)) {
nonAdminIBArray.push(nonAdminIB[prop]);
}
}
然后使用nonAdminIBArray
敲门。