user212421
user212421

Reputation: 31

Knockout JS: ko.applyBindings is not a function

I'm trying to use Knockout js in a simple web application. Here's my dummy javascript code:

function MainViewModel() {
    this.myText = ko.observable('Hello world');
}
var MainViewModelInstance = new MainViewModel();
ko.applyBindings(MainViewModelInstance);

But when I run the index.html, the debug console says "ko.applyBindings is not a function"!

Help! Thanks

Upvotes: 1

Views: 2618

Answers (1)

Joel Cunningham
Joel Cunningham

Reputation: 13740

You have not included the link to the knockout.js library in your source code or the link is wrong. Fix this and it will work.

<script src="/scripts/knockout-2.0.0.js" type="text/javascript"></script>

Where the /scripts directory is the location on the server where knockoutjs resides.

EDIT

Here is an example of your code that works.

<html>
    <head>
        <script src="knockout-2.0.0.js" type="text/javascript"></script>
    </head>
    <body>

        <script type="text/javascript">

            function MainViewModel() {
                this.myText = ko.observable('Hello world');
            }
            var MainViewModelInstance = new MainViewModel();
            ko.applyBindings(MainViewModelInstance);

        </script>

    </body>
</html>

Upvotes: 4

Related Questions