Vlad Dekhanov
Vlad Dekhanov

Reputation: 1086

Knockout simple example not working

I can't connect Knockout.js to my project. jQuery and knockout store in "Scripts" folder. What's wrong? When I run a project, i see only "First name" and "Last name". Blank instead of "Bert" and "Bergton".
Source file:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width" />
    <title></title>
    <link href="/Content/site.css" rel="stylesheet"/>

    <script src="/Scripts/modernizr-2.6.2.js"></script>

    <script src="/Scripts/jquery-1.8.2.js"></script>

</head>
<body>


<p>First name: <strong data-bind="text: firstName"></strong></p>
<p>Last name: <strong data-bind="text: lastName"></strong></p>
<script type="text/javascript" src="Scripts/knockout-2.2.0.js">
function AppViewModel() {
    this.firstName = "Bert";
    this.lastName = "Bergton";
}

// Activates knockout.js
ko.applyBindings(new AppViewModel());

</script>
</body>
</html>

Please, help me.

Upvotes: 0

Views: 89

Answers (1)

Judah Gabriel Himango
Judah Gabriel Himango

Reputation: 60001

You didn't close your Knockout script tag.

Here's the corrected code:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width" />
    <title></title>
    <link href="/Content/site.css" rel="stylesheet"/>

    <script src="/Scripts/modernizr-2.6.2.js"></script>

    <script src="/Scripts/jquery-1.8.2.js"></script>

</head>
<body>


<p>First name: <strong data-bind="text: firstName"></strong></p>
<p>Last name: <strong data-bind="text: lastName"></strong></p>
<script type="text/javascript" src="Scripts/knockout-2.2.0.js"></script> 
<script type="text/javascript">
    function AppViewModel() {
        this.firstName = "Bert";
        this.lastName = "Bergton";
    }

    // Activates knockout.js
    ko.applyBindings(new AppViewModel());

</script>
</body>
</html>

Upvotes: 1

Related Questions