1

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 ?

2 Answers 2

1

$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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that is it. I thought that mistake is in the first part of condition, because I try it with solution: <!-- ko if: $parent.sequences.length-1 === 3 --> and it does not work.
1

You can use the visible binding for this:

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

1 Comment

Thank you, it also works, but 'namesv' answer the best fit my question and I accept his answer :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.