Possible Duplicate:
KnockoutJS: ko.applyBindings to partial view?
I use knockout with jQuery. As result of the basic example below, the text "Planet Earth"
is displayed fine (so knockout added properly and works!), but "Planet2 Earth2"
is not displayed. Also, alert('alert2');
is fired, but alert('alert3');
is not.
Could anybody explain me why?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="Scripts/jquery-1.9.0.min.js" type="text/javascript"></script>
<script src="Scripts/knockout-2.2.1.js" type="text/javascript"></script>
<script>
$(document).ready(function () {
alert("alert1");
var viewModel = {
firstName: ko.observable("Planet"),
lastName: ko.observable("Earth")
};
viewModel.fullName = ko.dependentObservable(function () {
return viewModel.firstName() + " " + viewModel.lastName();
});
alert("alert2");
ko.applyBindings(viewModel);
alert('alert3');
var viewModel2 = {
firstName2: ko.observable("Planet2"),
lastName2: ko.observable("Earth2")
};
viewModel2.fullName2 = ko.dependentObservable(function () {
return viewModel2.firstName2() + " " + viewModel2.lastName2();
});
ko.applyBindings(viewModel2);
});
</script>
</head>
<body>
<div style="border:1px solid red;">
<p>First name: <input data-bind='value: firstName' /></p>
<p>Last name: <input data-bind='value: lastName' /></p>
<h2>Hello, <span data-bind='text: fullName'> </span>!</h2>
</div>
<div style="border:1px solid green;">
<p>First name: <input data-bind='value: firstName2' /></p>
<p>Last name: <input data-bind='value: lastName2' /></p>
<h2>Hello, <span data-bind='text: fullName2'> </span>!</h2>
</div>
</body>
</html>