gpt4 book ai didi

knockout.js - knockout foreach 绑定(bind)中的 $index+1

转载 作者:行者123 更新时间:2023-12-03 06:01:16 27 4
gpt4 key购买 nike

我需要显示$index+1在一个表中。

如果我只使用$index所有元素都从0开始,我需要从1开始。

这是 knockout 的文档:http://knockoutjs.com/documentation/foreach-binding.html

在那里你可以找到这个例子:

<h4>People</h4>
<ul data-bind="foreach: people">
<li>
Name at position <span data-bind="text: $index"> </span>:
<span data-bind="text: name"> </span>
<a href="#" data-bind="click: $parent.removePerson">Remove</a>
</li>
</ul>
<button data-bind="click: addPerson">Add</button>

因此它将显示以下内容:

人物

Name at position 0: Bert Remove

Name at position 1: Charles Remove

Name at position 2: Denise Remove

我真的需要这个仅用于显示目的。

Name at position 1: Bert Remove

Name at position 2: Charles Remove

Name at position 3: Denise Remove

我尝试过但没有成功 <span data-bind="text: ($index + 1)"> </span>

最佳答案

$index 是一个可观察的。所以你需要这样使用它:

<span data-bind="text: ($index() + 1)"> </span>

关于knockout.js - knockout foreach 绑定(bind)中的 $index+1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17734415/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com