The knockoutjs foreach alias does not work as explained in the knockoutjs docs halfway down the page under:
Note 3: Using “as” to give an alias to “foreach” items
Here is a simple jsFiddle showing the error. The console says that the alias is not defined although I’ve followed the knockoutjs example…
HTML
<h3>This works</h3>
<ul data-bind="foreach: people">
<li>
<span data-bind="text: $data.name"></span>
<span data-bind="text: $data.age"></span>
</li>
</ul>
<h3>This doesn't work</h3>
<ul data-bind="foreach: { data: people, as: 'person' }">
<li>
<span data-bind="text: person.name"></span>
<span data-bind="text: person.age"></span>
</li>
</ul>
JS
var data = [
{ name: 'Bob', age: 35 },
{ name: 'Sue', age: 24 },
{ name: 'Rick', age: 57 }
];
var model = function( data ){
this.people = ko.observableArray( data );
}
ko.applyBindings( new model( data ) );
Am I just not understanding the example and doing something wrong? It’s been known to happen, lol…
Upgrade to knockout 2.2.0 and it works. Upgraded fiddle here.
Your bindings are fine – leave them as-is: