我可以在http://learn.knockoutjs.com/#/?tutorial=intro进行在线教程,但是当我尝试在空的ASP.NET项目中在HTML页面中执行等效操作时,它将无法工作。为什么呢?
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="Scripts/knockout-2.3.0.js"></script>
<script type="text/javascript">
function AppViewModel() {
this.firstName = "Bert";
this.lastName = "Bertington";
}
ko.applyBindings(new AppViewModel());
</script>
</head>
<body>
<p>First name: <strong data-bind="text: firstName"></strong></p>
<p>Last name: <strong data-bind="text: lastName"></strong></p>
</body>
</html>
答案 0 :(得分:3)
我查看了另一个链接,如果我将ko.appyBindings移到底部,它就有效:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="Scripts/knockout-2.3.0.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">
function AppViewModel() {
this.firstName = "Bert";
this.lastName = "Bertington";
}
ko.applyBindings(new AppViewModel());
</script>
</body>
</html>