Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

KnockoutJS template inside foreach

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" },
    ]
});​
like image 709
Jason More Avatar asked Dec 19 '12 16:12

Jason More


1 Answers

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/

like image 98
Artem Vyshniakov Avatar answered Sep 20 '22 01:09

Artem Vyshniakov