I am facing an issue where I want to pass a specific object within an ObservableArray
defined in my view model to a template along with the index number.
This is how it looks in my view:
<!-- ko template: { name: "fooTemplate", with: FooCycles()[0] } --><!-- /ko -->
<script id="fooTemplate" type="text/html">
//some HTML for an individual FooCycle here
</script>
However, I encountered an error stating
Uncaught ReferenceError: Unable to process binding "template: function (){return { name:"fooTemplate",with:FooCycles()[0]} }"
. Even though under the with binding, it still focuses on the parent VM it belongs to in the JS debugger (Chrome).
In my model definition, I have access to a specific array object that is used for various ko.computed
properties:
var fstsum = parseFloat(self.FooCycles()[0].sum());
var sndsum = parseFloat(self.FooCycles()[1].sum());
I can successfully use FooCycles
in a foreach
:
<!-- ko foreach: FooCycles -->
<div class="item">
<!-- ko template: { name: "fooTemplate", with: $data } --><!-- /ko -->
</div>
<!-- /ko -->
Accessing FooCycles()[0]
works in JavaScript, but not in Knockout.js. Is there a way to retrieve an array object with an index in Knockout?