I'm trying to use a knockoutjs template inside of a foreach, and can't figure out what the data member needs to be:
JSFiddle
HTML
<script type="text/html" id="userPicture-template">
<p data-bind="text: testText"></p>
</script>
<div >
<ul data-bind="foreach: voters">
<li data-bind="template: { name: userPicture-template, data: $data }">
</li>
</ul>
</div>
<div data-bind="foreach: voters" >
<div>
<div data-bind="template: { name: userPicture-template, data: $data }">
</div>
</div>
</div>
Javascript
ko.applyBindings({
voters: [
{ testText: "hello" },
{ testText: "world" },
]
});
You didn't wrap template name with quotes:
<div >
<ul data-bind="foreach: voters">
<li data-bind="template: { name: 'userPicture-template', data: $data }">
</li>
</ul>
</div>
<div data-bind="foreach: voters" >
<div data-bind="css: questionClasses">
<div data-bind="template: { name: 'userPicture-template', data: $data }">
</div>
</div>
</div>
Also there is no questionClasses
field in your view model.
Here is working fiddle: http://jsfiddle.net/ywqct/2/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With