Reputation: 1902
I am the process of learning Knockout JS. I'm going through some of John Papa's tutorial on Pluralsight regarding Knockout and Javascript.
The <div data-bind="text: people().length" class="label">
section seems to be working no problems. However the foreach binding seems to have issue.
What I've tried and done:
$(function () {
var Person = function (name) {
this.name = ko.observable(name);
};
var viewModel = (function () {
var p1 = new Person('Da Vinci');
var p2 = new Person('Michangelo');
var people = ko.observableArray([p1, p2]);
return {
people: people
};
})();
ko.applyBindings(viewModel);
});
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Knockout</title>
<link href="../Content/bootstrap.css" rel="stylesheet" />
</head>
<body>
<h1>Hello Knockout!</h1>
<section>
Records:
<div data-bind="text: people().length" class="label">
<ul data-bind="foreach: people">
<li>
<span data-bind="text: name"></span>
</li>
</ul>
</div>
</section>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/knockout/knockout-2.2.1.js"></script>
<script src="../Js/ViewModel/AuthorCart.js"></script>
</body>
</html>
I am getting the following result but I'm expecting the two person name ('Da Vinci' and 'Michangelo') to appear underneath the records.
Records: 2
Upvotes: 0
Views: 915
Reputation: 9858
<div data-bind="text: people().length" class="label">
<ul data-bind="foreach: people">
<li>
<span data-bind="text: name"></span>
</li>
</ul>
</div>
When you bind the count, you replace the entire contents of the div with "2", that likely includes overwriting the ul contained therein so that it is never able to be bound.
Can you try this:
<span data-bind="text: people().length" class="label"></span>
<ul data-bind="foreach: people">
<li>
<span data-bind="text: name"></span>
</li>
</ul>
Upvotes: 2
Reputation: 114792
The text
binding that you have on the containing div is overwriting the children of the element, so your foreach
is gone.
You would want to close off the container first:
<div data-bind="text: people().length" class="label"></div>
<ul data-bind="foreach: people">
<li>
<span data-bind="text: name"></span>
</li>
</ul>
Upvotes: 3