Knockout.js 击倒让每个人都开始工作

Knockout.js 击倒让每个人都开始工作,knockout.js,foreach,Knockout.js,Foreach,我有一个包含3个音符的物体 { notes:[ { id:{type:"integer",value:"215356161"}, body:"", author_id:"980766", subject_id:"201674760", created_at:{date:"2014-03-24 16:50:14",timezone_type:2,timezone:"Z"}, attachments:{ type:"array",

我有一个包含3个音符的物体

{
 notes:[
 {
   id:{type:"integer",value:"215356161"},
   body:"", 
   author_id:"980766",
   subject_id:"201674760",
   created_at:{date:"2014-03-24 16:50:14",timezone_type:2,timezone:"Z"},
   attachments:{
         type:"array",
         attachments:[{
             id:{type:"integer",value:"77791298"},
             url:"https://fdgsgds.highrisehq.com/files/777915446298",
             name:"nav-rubik-03.mp3",
             size:"13954"
       ]} //typo
   }
 },
 {
   id:{type:"integer",value:"215356129"},
   body:"Test",author_id:"980766",
   subject_id:"201674760",
   created_at:{date:"2014-03-24 16:50:08",timezone_type:2,timezone:"Z"}
 },
 {

 ...
 }]
}
我想重复一下

第二个foreach不起作用

<div data-bind="foreach: appModel.issue().highriseNotes()">
    <p data-bind="text:$data.created_at.date"></p>

    <p data-bind="text:$data.body"></p>
    <!-- UPDATE, The Solution was to check if there were attachments at all -->
    <!-- ko if:$data.attachments -->
   <div data-bind="foreach: $data.attachments.attachments)">
       <p data-bind="text:$data">   </p>                                                        
   </div>
    <!-- /ko -->
</div>

如何迭代“附件”中的“附件”

在下面的示例中,我将
notes
数组传递到
ko.observearray


解决方案:

数组中的一个元素缺少attachment属性,导致绑定失败

一些解决方案

一,。将内部foreach包裹在if绑定中

<!-- ko if:$data.attachments -->
  <div data-bind="foreach..." />
<!-- /ko -->
三,。将绑定更改为默认为空对象

<div data-bind="foreach: ($data.attachments || {}).attachments)"> 


您的代码中有几个拼写错误,您能清理一下吗?没有第二个foreach!现在有了,请提及其他几个打字错误中的任何一个好吗?我没有看到一个在这里,你去,更新了问题的答案与2个额外的建议生成。“如果”的装订对我来说有点粗糙;)
{
  notes:[
  {
     ...
     attachments: {
       type:"array",
       attachments:[]           
     }
  }
}
<div data-bind="foreach: ($data.attachments || {}).attachments)">