Search code examples
knockout.jsknockout-2.0knockout-mapping-pluginknockout-validationknockout-mvc

How to use '&' in if condition with in Virtual element knockout?


I want to implement the virtual element like

<!-- ko if:  $index() > 9 && $index() < 20 -->

but it does not executed. Please help me for it.


Solution

  • Check this out: http://jsfiddle.net/y3KV2/

    What you had there works, but it might be that you are missing something else, so I made this little small simple example so you can see how it works.

    <div data-bind="foreach: data"> 
        <!-- ko if: $index() > 9 && $index() < 20  -->
            test <span data-bind="text: $index()"></span> 
        <!-- /ko -->
    </div>
    
    
    var vm = function () {
        var self = this;
        self.data = ko.observableArray([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]);
    }
    
    var s = new vm();
    
    ko.applyBindings(s);