[英]How to iterate array of arrays using knockout data-bind foreach?
I have an array of arrays that contain objects, and im looking for a way to loop over the inner arrays of the array, something like this: 我有一个包含对象的数组数组,我正在寻找一种循环遍历数组内部数组的方法,如下所示:
<div data-bind="foreach: questions">
<div data-bind="foreach: subArray of questions">
<span data-bind="text: Title"></span>
<span data-bind="text: Answer"></span>
</div>
</div>
How can i access the inner arrays of the questions observableArray so i can loop over the elements? 我如何访问observableArray问题的内部数组,以便可以遍历元素?
thanks in advance for any help! 在此先感谢您的帮助!
I have updated your fiddle . 我已经更新了你的小提琴 。 To use inner loop knockout has provided various properties like
$data
, to use outer loop $parent
. 使用内部循环敲除提供了各种属性,例如
$data
,使用外部循环$parent
。
<div class="answers" data-bind="foreach: questions">
<div data-bind="foreach: $data ">
<p data-bind="text: Title"></p>
<p data-bind="text: Answer"></p>
</div>
</div>
It's all in the docs, you just have to access inner arrays with $data
: http://knockoutjs.com/documentation/foreach-binding.html 所有这些都在文档中,您只需要使用
$data
访问内部数组即可: http : //knockoutjs.com/documentation/foreach-binding.html
<div class="answers" data-bind="foreach: questions">
<div data-bind="foreach: $data ">
<p data-bind="text: Title"></p>
<p data-bind="text: Answer"></p>
</div>
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.