【发布时间】:2020-06-22 08:58:09
【问题描述】:
这里是初学者并尝试运行 Knockout js。 使用 debian 10。
我做了什么: -installed Knockoutjs(凉亭安装淘汰赛) - 在 Visual Studio 代码上使用了 Knockout
我的代码:
<html>
<head>
<title>Knockout Test</title>
</head>
<body>
<p>First name: <input data-bind="value: firstName" /></p>
<p>Last name: <input data-bind="value: lastName" /></p>
<p>Full name: <strong data-bind="text: fullName"></strong></p>
<button data-bind="click: capitalizeLastName">Go caps</button>
<script type="text/javascript" src="js/knockout-3.5.1.js"></script>
<script type="text/javascript">
function AppViewModel() {
this.firstName = ko.observable("Bert");
this.lastName = ko.observable("Bertington");
/*
this.fullName = ko.computed(function() {
return this.firstName() + " " + this.lastName();
}, this);
this.capitalizeLastName = function() {
var currentVal = this.lastName(); // Read the current value
this.lastName(currentVal.toUpperCase()); // Write back a modified value
};*/
}
</script>
</body>
</html>
【问题讨论】:
-
你需要将构造函数的实例绑定到DOM:
ko.applyBindings(new AppViewModel())
标签: html input knockout.js observable