如何使用具有特殊第一个元素的 foreach? [英] How to use foreach with a special first element?

查看:19
本文介绍了如何使用具有特殊第一个元素的 foreach?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个可观察的数组

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}} 至关重要,因为 Knockout 文档中提到的好处.

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 中的 foreach 绑定成为可能2.0:

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/

这篇关于如何使用具有特殊第一个元素的 foreach?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆