Mihai Dabiste
Mihai Dabiste

Reputation: 157

Knockout foreach not working

I have the following HTML:

<!-- ko.foreach: properties -->
<span data-bind="text: $data.Name"></span>
<span data-bind="text: $data.Age"></span>
<!-- /ko -->

And this javascript:

function MyViewModel() {
    var self = this;
    self.properties = ko.observableArray([
        {Name: "John", Age: 32},
        {Name: "Steve", Age: 23}
    ]);
}
ko.applyBindings(new MyViewModel());

Here's the jsfiddle http://jsfiddle.net/cFB5B/

Why doesn't the foreach work?

Upvotes: 1

Views: 1773

Answers (2)

delixfe
delixfe

Reputation: 2491

You have a type . to much in the foreach declaration:

Instead

<!-- ko.foreach: properties -->

it should be

<!-- ko foreach: properties -->

Upvotes: 4

Richard Dalton
Richard Dalton

Reputation: 35803

It's ko foreach, not ko.foreach when binding a foreach inside a HTML comment (Note 4):

<!-- ko foreach: properties -->
<span data-bind="text: Name"></span>
<span data-bind="text: Age"></span>
<!-- /ko -->

http://jsfiddle.net/cFB5B/1/

Also, you don't need to use the $data, but that wasn't causing the problem.

Upvotes: 6

Related Questions