如何使用带有特殊第一个元素的foreach? [英] How to use foreach with a special first element?
问题描述
如果我有一个可观察的数组
If I have an observable array
foos = [{ name: "a" }, { name: "b" }, { name: "c" }]
在我的视图模型上,我想呈现以下内容:
on my viewmodel, I would like to render the following:
<ul>
<li class="add-new-foo">Special stuff here</li>
<li>a</li>
<li>b</li>
<li>c</li>
</ul>
我和
<ul data-bind="template: { name: 'foo-template', foreach: foos }">
<li class="add-new-foo">Special stuff here</li>
</ul>
<script id="foo-template" type="text/html">
<li data-bind="text: name"></li>
</script>
但这最终将.add-new-foo
放在a,b,c之后.
But this ended up putting the .add-new-foo
after the a, b, c.
有什么想法吗?在我的情况下,使用Knockout foreach
代替jQuery模板的{{each}}
是至关重要的,因为好处淘汰赛文档中提到的.
Any ideas? In my case it's crucial to use Knockout foreach
instead of jQuery template's {{each}}
, because of the benefits mentioned in the Knockout docs.
推荐答案
在KO 1.3 2.0中,新的无容器控制流和foreach
绑定似乎将成为可能:
Looks like it's going to be possible with the new containerless control flow and foreach
binding in KO 1.3 2.0:
<ul>
<li>Static item</li>
<!-- ko foreach: products -->
<li data-bind="text: name"></li>
<!-- /ko -->
</ul>
有关详细信息,请参见此帖子: http ://blog.stevensanderson.com/2011/08/31/knockout-1-3-0-beta-available/
See this post for details: http://blog.stevensanderson.com/2011/08/31/knockout-1-3-0-beta-available/
这篇关于如何使用带有特殊第一个元素的foreach?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!