sofboy sofboy - 3 months ago 10
HTML Question

Knockout foreach won't show anything

I'm new at knockout, and when I want to use the foreach I only get 1 empty line of nothing, while there are 2 objects in my array?

This is my HTML code

<tbody data-bind="foreach: lijst">
<tr>
<td><input type="text" data-bind="value: naam"/></td>
<td><input type="number" data-bind="value: aantal"/></td>
</tr>
</tbody>

function Product(naam, aantal) {
var self = this;
self.naam = naam;
self.aantal = aantal;
}

function komaan() {
var self = this;
self.lijst = ko.observableArray([
new Product("Flesje Tonic", 6),
new Product("Gin", 6)
]);
}

ko.applyBindings(new komaan());

Answer

Most probably your knockout js library is not loaded properly.

Here is online example with your code that works as expected http://jsfiddle.net/wyeegtaL/1/

function Product(naam, aantal) {
    var self = this;
    self.naam = naam;
    self.aantal = aantal;
}

function komaan() {
    var self = this;
    self.lijst = ko.observableArray([
        new Product("Flesje Tonic", 6),
        new Product("Gin", 6)
    ]);
}

ko.applyBindings(new komaan());
Comments