Milos Milos - 3 months ago 11
Javascript Question

Knockout not observable array length inside if block

I have one 'not observable' array of objects in view-model and I want to render some div-s according to the array.

<div data-bind="foreach: sequences">
<!-- ko if: $parent.sequences.length-1 > $index -->
<div>Some content </div>
<!-- /ko -->
</div>


Code above should render div for all elements except the last one, but it does not work, I did not get any error, I do know what is happening ?

Answer

$index is an observable, so you need to use $index():

<div data-bind="foreach: sequences">
    <!-- ko if: $parent.sequences.length-1 > $index() -->
        <div>Some content </div>
        <div data-bind="text: $data"></div>
    <!-- /ko -->        
</div>

Demo JSFiddle.

Comments