在绑定中使用 $index 时,淘汰赛不评估表达式 [英] Knockout is not evaluating an expression when using $index in a binding
问题描述
为什么当我尝试使用knockout.js 使用$index 绑定一些文本时,我得到的是函数代码而不是数字?
Why is it, that when I try to use knockout.js to bind some text using $index, I get the code of a function instead of a number?
<tbody data-bind="foreach: MyList">
<tr>
<td><span data-bind="text: $index + 1"></span></td>
</tr>
</tbody>
我得到的不是 1、2、3 等,而是:
Instead of getting 1, 2, 3 etc., I get this:
你可以看到,通过上图中的最后一个字符,我的索引零被添加到 1.如果我从绑定中删除 '+1',我得到 0, 1, 2 而不是函数.
You can see, by the last character in the above image, that my index of zero is being added to 1. If I remove the '+ 1' from my binding, I get 0, 1, 2 instead of the function.
我如何告诉knockout来评估表达式?当我提交表单时,我遇到了同样的问题.我的字符串字段作为函数而不是值提交.
How do I tell knockout to evaluate the expression? I have the same issue when I submit the form. My string fields are being submitted as a function instead of the value.
推荐答案
$index 是一个 observable,它是一个函数.试试 <span data-bind="text: $index() + 1"></span>
$index is an observable, which is a function. Try <span data-bind="text: $index() + 1"></span>
这篇关于在绑定中使用 $index 时,淘汰赛不评估表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!