Js 使用 $index 和 if 绑定

我试图显示一些基于 $index的值的标记,我可以显示这个值,但是我似乎不能使用它与 if绑定,什么是最好的方法在这里?

<!-- ko if: $index===0 -->
<div>some mark up here</div>
<!-- /ko -->
42501 次浏览

$index is an observable, and observables are functions. When you use observables in an expression you must use the () form to access the value.

<!-- ko if: $index() === 0 -->

From the knockout bindings page

$index (only available within foreach bindings)

This is the zero-based index of the current array entry being rendered by a foreach binding. Unlike the other binding context properties, $index is an observable and is updated whenever the index of the item changes (e.g., if items are added to or removed from the array).

Example

<div data-bind="foreach: details.additionalDetails">
<!-- ko if: $index() !== 0 -->
<span> | </span>
<!-- /ko -->
<span data-bind="text: name"></span> <span data-bind="text: value"></span>
</div>

Results in

Model #: UAI5021 | Catalog #: UIOY786