由于绑定淘汰未定义嵌套foreach

时间:2014-08-08 18:54:42

标签: javascript knockout.js

所以我有html:

<div class="body" data-bind="foreach: { data: Sections }">
            <span data-bind="text: '(' + OrderQualifier + ') ' + Text">
            </span>
            <p data-bind="foreach: { data: Children, as: 'child' }">
                <fieldset class="section-edit" data-bind="visible: IsEditing">
                    <input type="text" data-bind="attr: {value: child.EditedText}" /><!-- child is undefined here even though I have it as my as binding on the above foreach-->
                    <button data-bind="event: {click: $root.addEdit}">Submit</button>
                </fieldset>
            </p> 
        </div>

我尝试在没有as绑定的情况下执行此操作,但是它从父节中提取值,该节也具有EditedText属性,并使用$ data获得相同的结果。

我试图绑定的数据(部分)看起来像:

[  
   {  
      "SectionID":1,
      "Text":"Parent text",
      "Html":null,
      "OrderQualifier":"1",
      "IsUserCreated":false,
      "Children":[  
         {  
            "SectionID":2,
            "Text":"Child text",
            "Html":null,
            "OrderQualifier":"1",
            "IsUserCreated":false,
            "EditCount":0,
            "ExplanationCount":0,
            "EvidenceCount":0,
            "IsEditing":true,
            "EditedText":"Child text"
         }
      ],
      "EditCount":0,
      "ExplanationCount":0,
      "EvidenceCount":0,
      "IsEditing":true,
      "EditedText":"Parent text"
   }
]

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

如果您想继续使用<p>代码

,请使用虚拟元素
<!-- ko foreach: { data: Children, as: 'child' } -->
        <p>
        ...
        </p> 
<!-- /ko -->

http://jsfiddle.net/cvtw3b2h/2/

答案 1 :(得分:0)

非常奇怪,但尝试更改

<p data-bind="foreach: { data: Children, as: 'child' }">
....
</p>

<div data-bind="...">...</div>

http://jsfiddle.net/cvtw3b2h/1/