Recently to use Knockoutjs in the project, so today I first studied the KNOCKOUTJS environment, and conducted a simple test.
First to http://knockoutjs.com/index.html download the latest version of the Knockoutjs, the author here to download is knockout-2.2.0.js. Then create a new. html file and add the following statement to the HTML document to import this JS:
<script type= "Text/javascript" src= "Knockout-2.2.0.js" ></script>
Here we are ready to write Knockoutjs code, we print "Hello world!!" For example, write the following code:
Copy Code code as follows:
<script type= "Text/javascript" src= "Knockout-2.2.0.js" ></script>
<span data-bind= "Text:helloworld" ></span>
<script type= "Text/javascript" >
var Appviewmodel = {
HelloWorld: ' Hello world!! '
};
Ko.applybindings (Appviewmodel);
</script>
Then use the browser to open this HTML file, you can see "Hello world!!" The words.
In the above code:
<span data-bind= "Text:helloworld" ></span>
Defines a span and uses Data-bind to bind the HelloWorld to a span so that the contents of the span are the contents of the HelloWorld variable.
Copy Code code as follows:
<script type= "Text/javascript" >
var Appviewmodel = {
HelloWorld: ' Hello world!! '
};
Ko.applybindings (Appviewmodel);
</script>
A appviewmodel is defined in the script, and a variable is defined for it: HelloWorld, its value is: Hello world!!, and then using the Ko.applybindings () method to activate Appviewmodel, This allows you to see this content in the Web page.
The above is just using Knockoutjs to do a very simple example, in the process of running this example, there is a small problem, I do not know why. The code previously written is:
Copy Code code as follows:
<script type= "Text/javascript" src= "Knockout-2.2.0.js" ></script>
<script type= "Text/javascript" >
var Appviewmodel = {
HelloWorld: ' Hello world!! '
};
Ko.applybindings (Appviewmodel);
</script>
<span data-bind= "Text:helloworld" ></span>
I put <script> in front of <span>, so that will not show any content, not very clear the reasons, but also please advise.