Knockout is not evaluating an expression when using $index in a binding

故事扮演 提交于 2019-11-28 00:47:17

$index is an observable, which is a function. Try <span data-bind="text: $index() + 1"></span>

If you use

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

and for example your index value is 2, the text of your span will be: 21 and not 3.

you should define a function in your viewModel, like this:

self.itemNumber = function(index) {
    return index + 1;
}

and then in your span you should do:

<span data-bind="text: $root.itemNumber($index())"></span>

I hope this will help :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!