基于第一个淘汰教程,为什么我的代码不运行? [英] Based on the first knockout tutorial, why is my code not running?
问题描述
我了解本教程在该页面上的工作原理,但我正在尝试在本地设置一个创建计算器,并且无法使knockout.js工作。它不像knockout.js在线教程初始化或填充。
I understand how the tutorial works on the page, but I'm trying to set one up locally to create a calculator and can't get knockout.js to work. It doesn't initialize or populate like the knockout.js online tutorial.
HTML
<html>
<head>
<script type="text/javascript" language="javascript" src="../knockout-2.1.0.js"></script>
<script type="text/JavaScript" language="javascript">
function AppViewModel() {
this.firstName = ko.observable("Bert");
this.lastName = ko.observable("Bertington");
}
// Activates knockout.js
ko.applyBindings(new AppViewModel());
</script>
</head>
<body class="calc" onLoad="createnav()">
<div id="all">
<div id="calc">
<p>First name: <strong data-bind="text: firstName"></strong></p>
<p>Last name: <strong data-bind="text: lastName"></strong></p>
<p>First name: <input data-bind="value: firstName" /></p>
<p>Last name: <input data-bind="value: lastName" /></p>
</div>
<div id="info">
<!-- outputs will be here -->
</div>
</div>
</body>
</html>
我正在使用knockout.js版本2.1.0。至于 src
的位置是正确的。
I'm using knockout.js version 2.1.0. As for location of the src
it is correct.
文件夹结构
----------
| Root |
----------_____________________
| |
------------------- ------------ ---------------
| knockout-2.1.0.js | | pphcalc | ___ | HeroPilot.asp |
------------------- ------------ ---------------
任何建议?
推荐答案
您正在使用标头脚本标记中的绑定,因此在运行ko.applyBindings(新的AppViewModel())行时,还没有任何要绑定的元素。
You are applying the bindings in a header script tag so there are not yet any elements to bind to at the point that your ko.applyBindings(new AppViewModel()) line runs.
您可以为JQuery Window.load函数提供回调,以确保在应用绑定之前正确加载所有内容。示例:
You can provide a callback for the JQuery Window.load function to ensure that everything is properly loaded before the bindings get applied. Example:
<script type="text/JavaScript" language="javascript">
function AppViewModel() {
this.firstName = ko.observable("Bert");
this.lastName = ko.observable("Bertington");
}
// Activates knockout.js
$(window).load(function() {
ko.applyBindings(new AppViewModel());
});
</script>
这篇关于基于第一个淘汰教程,为什么我的代码不运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!