0

I want to implement the virtual element like

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

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

nemesv
  • 138,284
  • 16
  • 416
  • 359
Shreyash Mahajan
  • 23,386
  • 35
  • 116
  • 188
  • can you post some more of your code around your foreach and some of your viewmodel. Better still replicate the issue on jsfiddle.net – Tanner Dec 05 '13 at 14:24

3 Answers3

1

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);
Armand
  • 9,847
  • 9
  • 42
  • 75
  • Hello Armand thanks for your suggestion. and Sorry from my side. but here i am pushing HTML code same like your in my cshtml page dynamically then my foreach does not work. if you know how to apply knockout on dynamic HTML pls let me know it would be very help full to me thanks.. – Shreyash Mahajan Dec 05 '13 at 13:31
  • @iDroidExplorer have a look at http://stackoverflow.com/questions/10826482/get-dynamically-inserted-html-to-work-with-knockoutjs – Armand Dec 06 '13 at 08:10
  • I have try with your given SO link. But its not working. Please help me for same. – Shreyash Mahajan Dec 06 '13 at 10:07
0

Use your DOM inspector to make sure that the comment node for your virtual element actually contains ampersands; the problem could either be that something that should be HTML-escaped isn't or that something that's already HTML-escaped is getting double-escaped.

Your server should be sending out

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

in order for the browser to create a comment node with the correct syntax for Knockout to parse. Use a tool like Curl to verify that it is.

ebohlman
  • 14,795
  • 5
  • 33
  • 35
  • Hello ebohlman thanks for your suggestion. and Sorry from my side. but here i am pushing HTML code same like in above reply by armand's code in page dynamically then my foreach does not work. if you know how to apply knockout on dynamic HTML pls let me know it would be very help full to me thanks.. – Shreyash Mahajan Dec 06 '13 at 07:07
0

Use a function ::

<!-- ko if: function() { myIndicesLoveMe($index) } -->

Then have the function somewhere and ensure it returns either True or False ::

myIndicesLoveMe = function(index) {
    return (index > 9 && index < 20);
}
beauXjames
  • 8,222
  • 3
  • 49
  • 66