简体   繁体   中英

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 ?

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

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM