Knockoutjs does not work

Asked

Viewed 115 times

2

Code:

<!DOCTYPE html>
<html>
<head>
<script type='text/javascript' src='js/knockout-3.2.0.js'></script>
<meta charset="UTF-8">
<title>Title of the document</title>
</head>

<body>

<script type="text/javascript">
function AppViewModel() {
    this.firstName = "Bert";
    this.lastName = "Bertington";
}


ko.applyBindings(new AppViewModel());

</script>

<p>First name: <input data-bind="value: firstName" /></p>
<p>Last name: <input data-bind="value: lastName" /></p>

</body>

</html>
  • 1

    I’ve tried to put the ko.applyBindings to the end of html? I believe that at the time it runs, the two input's have not yet been rendered, and then he does not find the elements to do the binding.

  • It worked! Thanks!

1 answer

2


Place ko.applyBindings at the bottom of the page or inside the

$(Function(){

});

Jquery.

In your case, do it this way.

<!DOCTYPE html>
<html>
<head>
<script type='text/javascript' src='http://knockoutjs.com/downloads/knockout-3.2.0.js'></script>
<meta charset="UTF-8">
<title>Title of the document</title>
</head>

<body>


<p>First name: <input data-bind="value: firstName" /></p>
<p>Last name: <input data-bind="value: lastName"/></p>

</body>

</html>

<script type="text/javascript">

function AppViewModel() {
    this.firstName = "Paulo";
    this.lastName = "Sousa";
}


ko.applyBindings(new AppViewModel());

</script>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.